In [1]:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import os
import random
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
from matplotlib import pyplot as plt
In [2]:
# Specify the path to the main directory
main_directory = '105_classes_pins_dataset'

# Initialize counts
folder_count = 0
total_file_count = 0

# Iterate through the main directory
for root, dirs, files in os.walk(main_directory):
    # Count folders
    folder_count += len(dirs)
    
    # Count files in each folder
    folder_file_count = len(files)
    total_file_count += folder_file_count
    
    # Print the folder name and the number of files in that folder
    print(f"Folder: {os.path.basename(root)}, Files: {folder_file_count}")

# Print the overall results
print(f"\nNumber of folders: {folder_count}")
print(f"Total number of files: {total_file_count}")
Folder: 105_classes_pins_dataset, Files: 0
Folder: pins_Adriana Lima, Files: 213
Folder: pins_Alex Lawther, Files: 152
Folder: pins_Alexandra Daddario, Files: 225
Folder: pins_Alvaro Morte, Files: 139
Folder: pins_alycia dabnem carey, Files: 211
Folder: pins_Amanda Crew, Files: 117
Folder: pins_amber heard, Files: 218
Folder: pins_Andy Samberg, Files: 196
Folder: pins_Anne Hathaway, Files: 203
Folder: pins_Anthony Mackie, Files: 124
Folder: pins_Avril Lavigne, Files: 162
Folder: pins_barack obama, Files: 119
Folder: pins_barbara palvin, Files: 197
Folder: pins_Ben Affleck, Files: 126
Folder: pins_Bill Gates, Files: 122
Folder: pins_Bobby Morley, Files: 138
Folder: pins_Brenton Thwaites, Files: 209
Folder: pins_Brian J. Smith, Files: 102
Folder: pins_Brie Larson, Files: 169
Folder: pins_camila mendes, Files: 162
Folder: pins_Chris Evans, Files: 166
Folder: pins_Chris Hemsworth, Files: 159
Folder: pins_Chris Pratt, Files: 176
Folder: pins_Christian Bale, Files: 154
Folder: pins_Cristiano Ronaldo, Files: 98
Folder: pins_Danielle Panabaker, Files: 181
Folder: pins_Dominic Purcell, Files: 146
Folder: pins_Dwayne Johnson, Files: 141
Folder: pins_Eliza Taylor, Files: 162
Folder: pins_Elizabeth Lail, Files: 158
Folder: pins_elizabeth olsen, Files: 221
Folder: pins_ellen page, Files: 188
Folder: pins_elon musk, Files: 135
Folder: pins_Emilia Clarke, Files: 210
Folder: pins_Emma Stone, Files: 139
Folder: pins_Emma Watson, Files: 211
Folder: pins_gal gadot, Files: 199
Folder: pins_grant gustin, Files: 183
Folder: pins_Gwyneth Paltrow, Files: 187
Folder: pins_Henry Cavil, Files: 195
Folder: pins_Hugh Jackman, Files: 179
Folder: pins_Inbar Lavi, Files: 127
Folder: pins_Irina Shayk, Files: 156
Folder: pins_Jake Mcdorman, Files: 159
Folder: pins_Jason Momoa, Files: 184
Folder: pins_jeff bezos, Files: 106
Folder: pins_Jennifer Lawrence, Files: 180
Folder: pins_Jeremy Renner, Files: 167
Folder: pins_Jessica Barden, Files: 141
Folder: pins_Jimmy Fallon, Files: 113
Folder: pins_Johnny Depp, Files: 182
Folder: pins_Josh Radnor, Files: 117
Folder: pins_Katharine Mcphee, Files: 177
Folder: pins_Katherine Langford, Files: 226
Folder: pins_Keanu Reeves, Files: 160
Folder: pins_kiernen shipka, Files: 203
Folder: pins_Krysten Ritter, Files: 171
Folder: pins_Leonardo DiCaprio, Files: 237
Folder: pins_Lili Reinhart, Files: 150
Folder: pins_Lindsey Morgan, Files: 169
Folder: pins_Lionel Messi, Files: 86
Folder: pins_Logan Lerman, Files: 212
Folder: pins_Madelaine Petsch, Files: 192
Folder: pins_Maisie Williams, Files: 193
Folder: pins_margot robbie, Files: 221
Folder: pins_Maria Pedraza, Files: 122
Folder: pins_Marie Avgeropoulos, Files: 161
Folder: pins_Mark Ruffalo, Files: 178
Folder: pins_Mark Zuckerberg, Files: 95
Folder: pins_Megan Fox, Files: 209
Folder: pins_melissa fumero, Files: 154
Folder: pins_Miley Cyrus, Files: 178
Folder: pins_Millie Bobby Brown, Files: 191
Folder: pins_Morena Baccarin, Files: 175
Folder: pins_Morgan Freeman, Files: 105
Folder: pins_Nadia Hilker, Files: 133
Folder: pins_Natalie Dormer, Files: 198
Folder: pins_Natalie Portman, Files: 166
Folder: pins_Neil Patrick Harris, Files: 116
Folder: pins_Pedro Alonso, Files: 125
Folder: pins_Penn Badgley, Files: 171
Folder: pins_Rami Malek, Files: 160
Folder: pins_Rebecca Ferguson, Files: 178
Folder: pins_Richard Harmon, Files: 148
Folder: pins_Rihanna, Files: 133
Folder: pins_Robert De Niro, Files: 156
Folder: pins_Robert Downey Jr, Files: 233
Folder: pins_Sarah Wayne Callies, Files: 159
Folder: pins_scarlett johansson, Files: 201
Folder: pins_Selena Gomez, Files: 186
Folder: pins_Shakira Isabel Mebarak, Files: 154
Folder: pins_Sophie Turner, Files: 204
Folder: pins_Stephen Amell, Files: 159
Folder: pins_Taylor Swift, Files: 131
Folder: pins_Tom Cruise, Files: 192
Folder: pins_tom ellis, Files: 180
Folder: pins_Tom Hardy, Files: 198
Folder: pins_Tom Hiddleston, Files: 181
Folder: pins_Tom Holland, Files: 189
Folder: pins_Tuppence Middleton, Files: 133
Folder: pins_Ursula Corbero, Files: 167
Folder: pins_Wentworth Miller, Files: 179
Folder: pins_Zac Efron, Files: 191
Folder: pins_Zendaya, Files: 138
Folder: pins_Zoe Saldana, Files: 186

Number of folders: 105
Total number of files: 17534
In [3]:
# Specify the path to the main directory
main_directory = '105_classes_pins_dataset'

# Get a list of all folders
all_folders = [folder for folder in os.listdir(main_directory) if os.path.isdir(os.path.join(main_directory, folder))]

# Randomly pick 10 folders
selected_folders = random.sample(all_folders, 10)

# Create a 2x5 grid for displaying images
fig, axes = plt.subplots(2, 5, figsize=(10, 5))

# Iterate through selected folders and display one random image from each folder
for i, folder in enumerate(selected_folders):
    folder_path = os.path.join(main_directory, folder)
    all_images = [img for img in os.listdir(folder_path) if img.endswith('.jpg')]
    
    # Randomly pick one image
    selected_image = random.choice(all_images)
    
    # Get the full path to the selected image
    image_path = os.path.join(folder_path, selected_image)
    
    # Read and display the image
    img = mpimg.imread(image_path)
    axes[i // 5, i % 5].imshow(img)
    axes[i // 5, i % 5].axis('off')
    
    # Extract label from folder name and drop the first 5 characters
    label = folder[5:]
    axes[i // 5, i % 5].set_title(label)

# Adjust layout and show the plot
plt.tight_layout()
plt.show()
In [4]:
#!pip install mtcnn
In [4]:
import matplotlib.patches as patches
from mtcnn import MTCNN

# Function to perform face detection and display the image with bounding boxes
def detect_and_display_faces(image_path):
    # Load the image
    img = cv2.imread(image_path)
    
    # Convert the image to RGB for compatibility with MTCNN
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    
    # Load the pre-trained MTCNN model
    detector = MTCNN()
    
    # Perform face detection
    faces = detector.detect_faces(img_rgb)
    
    # Display the original image
    fig, ax = plt.subplots()
    ax.imshow(img_rgb)
    
    # Add bounding boxes for each detected face
    for face in faces:
        x, y, w, h = face['box']
        rect = patches.Rectangle((x, y), w, h, linewidth=2, edgecolor='r', facecolor='none')
        ax.add_patch(rect)
    
    plt.axis('off')
    plt.show()

# Specify the path to the main directory
main_directory = '105_classes_pins_dataset'

# Get a list of all folders
all_folders = [folder for folder in os.listdir(main_directory) if os.path.isdir(os.path.join(main_directory, folder))]

# Randomly pick a folder
selected_folder = random.choice(all_folders)
folder_path = os.path.join(main_directory, selected_folder)

# Get a list of all images in the selected folder
all_images = [img for img in os.listdir(folder_path) if img.endswith('.jpg')]

# Randomly pick an image from the folder
selected_image = random.choice(all_images)
image_path = os.path.join(folder_path, selected_image)

# Perform face detection using MTCNN and display the result
detect_and_display_faces(image_path)
WARNING:tensorflow:From C:\Users\Ifham.Khan\anaconda3\lib\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.

WARNING:tensorflow:From C:\Users\Ifham.Khan\anaconda3\lib\site-packages\keras\src\backend.py:1398: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.

WARNING:tensorflow:From C:\Users\Ifham.Khan\anaconda3\lib\site-packages\keras\src\layers\pooling\max_pooling2d.py:161: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

1/1 [==============================] - 1s 1s/step
1/1 [==============================] - 1s 615ms/step
1/1 [==============================] - 0s 116ms/step
1/1 [==============================] - 0s 188ms/step
1/1 [==============================] - 0s 88ms/step
1/1 [==============================] - 0s 78ms/step
1/1 [==============================] - 0s 114ms/step
1/1 [==============================] - 0s 69ms/step
3/3 [==============================] - 1s 11ms/step
1/1 [==============================] - 0s 465ms/step
In [5]:
import os
import shutil

# Set the directory path
directory_path = 'working'

# Remove all files and subdirectories in the directory
for item in os.listdir(directory_path):
    item_path = os.path.join(directory_path, item)
    if os.path.isfile(item_path) or os.path.islink(item_path):
        os.unlink(item_path)
    elif os.path.isdir(item_path):
        shutil.rmtree(item_path)

print("Contents of the working directory cleared.")
Contents of the working directory cleared.
In [6]:
import shutil

# Source directory
source_directory = '105_classes_pins_dataset'

# Destination directory
destination_directory = 'working'

# Create train and test directories if not exist
train_directory = os.path.join(destination_directory, 'train')
os.makedirs(train_directory, exist_ok=True)


# Get a list of all folders in the source directory
all_folders = [folder for folder in os.listdir(source_directory) if os.path.isdir(os.path.join(source_directory, folder))]

# Iterate through each folder
for folder in all_folders:
    folder_path = os.path.join(source_directory, folder)
    
    # Get a list of all images in the folder
    all_images = [img for img in os.listdir(folder_path) if img.endswith('.jpg')]
    
    # Create a label for the folder (drop the first 5 characters)
    label = folder[5:]
    
    # Shuffle the images
    random.shuffle(all_images)
    
    # Move the first 85 images to the train directory
    for i in range(85):
        image_path = os.path.join(folder_path, all_images[i])
        destination_path = os.path.join(train_directory, label, all_images[i])
        os.makedirs(os.path.dirname(destination_path), exist_ok=True)
        shutil.copy(image_path, destination_path)
    

print("Data splitting completed.")
Data splitting completed.
In [7]:
from matplotlib.patches import Rectangle
from mtcnn.mtcnn import MTCNN

# Directory paths
train_directory = 'working/train'

# Get a list of all folders in the train directory
all_folders = [folder for folder in os.listdir(train_directory) if os.path.isdir(os.path.join(train_directory, folder))]

# Randomly select two folders
selected_folders = random.sample(all_folders, 2)

# Initialize MTCNN detector
detector = MTCNN()

# Iterate through selected folders
for folder in selected_folders:
    folder_path = os.path.join(train_directory, folder)
    
    # Get a list of all images in the folder
    all_images = [img for img in os.listdir(folder_path) if img.endswith('.jpg')]
    
    # Randomly select one image
    selected_image = random.choice(all_images)
    image_path = os.path.join(folder_path, selected_image)
    
    # Read the image
    image = cv2.imread(image_path)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # Detect faces using MTCNN
    faces = detector.detect_faces(image)
    
    # Display image with bounding boxes around detected faces
    plt.figure(figsize=(8, 3))
    
    # Display original image
    plt.subplot(1, 2, 1)
    plt.imshow(image_rgb)
    plt.title('Original Image')
    
    # Display image with bounding boxes
    plt.subplot(1, 2, 2)
    plt.imshow(image_rgb)
    
    for face in faces:
        x, y, width, height = face['box']
        rect = Rectangle((x, y), width, height, fill=False, color='red')
        plt.gca().add_patch(rect)
        
        # Display additional keypoints
        for key, value in face['keypoints'].items():
            plt.scatter(value[0], value[1], s=30, color='blue', marker='o')
            plt.text(value[0] + 5, value[1], key, color='blue')
    
    plt.title('Detected Faces with Keypoints')
    plt.show()

    # Display metadata of detected faces
    print(f"Metadata of detected faces in {folder}/{selected_image}:")
    for i, face in enumerate(faces):
        print(f"Face {i + 1}:")
        print(f"   Confidence: {face['confidence']:.2f}")
        print(f"   Bounding Box: {face['box']}")
        print(f"   Keypoints: {face['keypoints']}")
        print()
1/1 [==============================] - 0s 372ms/step
WARNING:tensorflow:5 out of the last 13 calls to <function Model.make_predict_function.<locals>.predict_function at 0x0000019C9A371B80> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
1/1 [==============================] - 0s 339ms/step
1/1 [==============================] - 0s 47ms/step
1/1 [==============================] - 0s 63ms/step
1/1 [==============================] - 0s 64ms/step
1/1 [==============================] - 0s 65ms/step
1/1 [==============================] - 0s 70ms/step
1/1 [==============================] - 0s 62ms/step
WARNING:tensorflow:5 out of the last 13 calls to <function Model.make_predict_function.<locals>.predict_function at 0x0000019C9A3B9550> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
3/3 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 379ms/step
Metadata of detected faces in Chris Hemsworth/Chris Hemsworth152_415.jpg:
Face 1:
   Confidence: 1.00
   Bounding Box: [40, 38, 155, 205]
   Keypoints: {'left_eye': (89, 107), 'right_eye': (162, 109), 'nose': (131, 146), 'mouth_left': (93, 188), 'mouth_right': (155, 189)}

1/1 [==============================] - 0s 53ms/step
1/1 [==============================] - 0s 57ms/step
1/1 [==============================] - 0s 78ms/step
1/1 [==============================] - 0s 41ms/step
1/1 [==============================] - 0s 45ms/step
1/1 [==============================] - 0s 64ms/step
1/1 [==============================] - 0s 54ms/step
1/1 [==============================] - 0s 41ms/step
1/1 [==============================] - 0s 74ms/step
1/1 [==============================] - 0s 78ms/step
Metadata of detected faces in Stephen Amell/Stephen Amell188_4150.jpg:
Face 1:
   Confidence: 1.00
   Bounding Box: [32, 48, 148, 194]
   Keypoints: {'left_eye': (87, 122), 'right_eye': (153, 120), 'nose': (133, 157), 'mouth_left': (98, 195), 'mouth_right': (152, 194)}

In [9]:
#!pip install facenet-pytorch
In [8]:
from facenet_pytorch import MTCNN, InceptionResnetV1, extract_face
from PIL import Image

# Function to process image, detect face, and calculate embeddings
def process_image(image_path, face_detector, face_embedder, label):
    # Read the image
    image = cv2.imread(image_path)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # Detect faces using MTCNN
    boxes, probs = face_detector.detect(image_rgb)

    if boxes is not None:
        # Display label of the image using the folder it is picked up from
        plt.title(f"Label: {label}")

        # Display original image
        plt.imshow(Image.fromarray(image_rgb))

        # Draw a rectangle around each detected face on the original image
        for i, box in enumerate(boxes):
            x, y, w, h = box
            # Draw a rectangle around the detected face with thinner lines
            cv2.rectangle(image_rgb, (int(x), int(y)), (int(x + w), int(y + h)), (255, 0, 0), 1)

        # Display the image with the rectangles around detected faces
        plt.imshow(Image.fromarray(image_rgb))

        plt.axis('off')
        plt.show()

        # Apply transformations and normalization
        transformed_faces = [face_embedder(extract_face(image_rgb, box).unsqueeze(0)) for box in boxes]

        # Convert PyTorch tensor to NumPy array
        transformed_faces_np = [transformed_face.squeeze().detach().numpy() for transformed_face in transformed_faces]

        # Print the embeddings
        for i, embeddings in enumerate(transformed_faces_np):
            print(f"Embeddings for Detected Face {i + 1}:", embeddings)

# Directory paths
train_directory = 'working/train/'

# Randomly pick a folder
selected_folder = random.choice(os.listdir(train_directory))
selected_folder_path = os.path.join(train_directory, selected_folder)

# Randomly pick one file from the selected folder
file = random.choice(os.listdir(selected_folder_path))
file_path = os.path.join(selected_folder_path, file)

# Initialize MTCNN for face detection with adjusted parameters
face_detector = MTCNN(margin=20, post_process=False, select_largest=False)
   
# Initialize InceptionResnetV1 for face embedding
face_embedder = InceptionResnetV1(pretrained='vggface2').eval()

# Process image, detect faces, calculate embeddings, and display results
process_image(file_path, face_detector, face_embedder, label=selected_folder)
Embeddings for Detected Face 1: [ 5.71297370e-02 -2.67934948e-02  3.81031856e-02  2.27163546e-02
  3.57267410e-02 -6.98381066e-02 -2.50966903e-02 -3.76983196e-03
 -2.96353945e-03 -5.07798344e-02 -8.72919708e-02 -6.77055344e-02
 -9.40293223e-02 -2.22640000e-02  7.56724328e-02 -2.69111544e-02
  5.44652343e-04  1.07508589e-04  3.00200358e-02  2.49170102e-02
  1.88040566e-02  2.91414633e-02 -5.01331575e-02 -1.18766641e-02
  1.39893349e-02 -3.66233126e-03  7.53469244e-02 -1.79457758e-02
  2.41288971e-02 -8.23454484e-02 -2.90093310e-02  5.40296882e-02
  2.29179431e-02  4.32333685e-02  6.66124374e-02  1.38067510e-02
 -4.40900922e-02  3.39963213e-02 -3.49919535e-02  2.30008345e-02
  8.09066556e-03  5.58163133e-03 -8.46079434e-04 -1.09960034e-01
 -2.18510404e-02 -6.12194985e-02  2.36308556e-02 -3.42507139e-02
 -4.37819436e-02  3.52482423e-02  1.08578905e-01  6.88848719e-02
  5.53716980e-02  7.22644106e-02 -8.98449216e-03 -6.90328181e-02
  4.13697958e-02  6.15122542e-02  3.99996946e-03  2.58536022e-02
 -2.63578054e-02 -1.01613522e-01  6.42609447e-02 -2.93224715e-02
  5.77571848e-03  3.06629874e-02  6.16656207e-02  3.42602879e-02
  2.49051359e-02 -8.20538551e-02  2.25909501e-02 -8.62764716e-02
 -6.27406081e-03 -1.81139708e-02 -1.16130086e-02  3.07787713e-02
  8.47872794e-02  2.21681464e-02 -1.50854737e-02  6.55373335e-02
  2.66668741e-02  6.11215122e-02  2.07586400e-02 -2.35098116e-02
 -1.82960592e-02  3.43521982e-02  3.36214378e-02 -1.07448502e-02
  7.47946836e-03  1.11695327e-01 -2.64298003e-02  5.35408147e-02
  5.71246259e-03 -4.95980978e-02 -5.93818538e-02  1.94376912e-02
 -3.61155495e-02  2.19532959e-02 -8.84989649e-02 -1.02047943e-01
 -3.03038526e-02 -3.96082588e-02  3.93114937e-03 -5.22588491e-02
  4.90954965e-02  1.71754826e-02  1.32309020e-01  1.08675109e-02
 -2.96060345e-04 -2.63486300e-02 -3.43204252e-02 -8.82319659e-02
  6.41116221e-03  1.05120335e-02 -1.84318447e-03  2.72151846e-02
  6.81786984e-03 -2.93561164e-02  1.80993375e-04  3.43012288e-02
 -8.48892704e-02 -5.98186860e-03  1.16549265e-02  5.41776419e-02
 -2.49393675e-02 -1.00599863e-02 -1.46408826e-02 -8.68028402e-02
 -1.24875475e-02  2.34454516e-02  2.06508040e-02  4.11290936e-02
  1.87859703e-02 -9.13568586e-02  1.99332386e-02  2.09055096e-02
  2.44836584e-02 -5.91235375e-03 -2.88535021e-02 -8.15733224e-02
  7.70698339e-02 -1.89777352e-02 -3.01886294e-02  1.73313282e-02
 -2.85990797e-02  1.81605387e-02 -2.57706512e-02 -1.65923629e-02
 -7.19916541e-04 -4.15094569e-03 -7.46959541e-03  3.93360667e-02
 -7.97324255e-02 -1.59871373e-02 -6.74565211e-02  7.26374332e-03
  5.65960780e-02  1.70974396e-02  9.78219323e-03  3.48697975e-02
 -3.86136235e-03 -2.11567767e-02  3.43021303e-02 -5.70137687e-02
 -2.99644116e-02  1.09251831e-02 -1.02776110e-01  2.93914881e-02
  4.26594727e-02  5.34811104e-03 -1.85193326e-02 -4.54982035e-02
 -2.71403641e-02 -8.55365619e-02 -1.27494885e-02 -4.46985066e-02
 -5.37515692e-02 -1.10088654e-01 -1.70345753e-02 -2.71163173e-02
 -3.45073938e-02  9.23171341e-02 -1.69338807e-02 -1.95366200e-02
 -1.63072608e-02  1.59206185e-02 -6.38923645e-02 -3.99290621e-02
  7.94148771e-04  1.07785262e-01 -9.50809196e-03  3.53800394e-02
 -2.50735115e-02  1.16082884e-01 -2.18565818e-02  3.17009129e-02
 -2.07136609e-02 -1.01471636e-02 -3.60553600e-02 -3.15122865e-02
 -2.85117188e-04 -2.75774952e-02 -2.38530338e-02  1.24528721e-01
 -4.65829596e-02 -9.19998065e-02  1.44233322e-02  6.61518648e-02
 -8.79022554e-02  9.18559507e-02  3.55658447e-03 -2.76046898e-02
 -5.65646328e-02  1.86320003e-02 -7.66579136e-02 -6.59664581e-03
  2.15541087e-02  5.29130213e-02 -2.75974162e-02  6.87111989e-02
  1.59375602e-03 -6.36594975e-03 -2.47926991e-02  1.68524273e-02
 -2.79625822e-02  3.38432007e-02  1.12575255e-02  5.33211837e-03
 -1.66061819e-02  2.10318379e-02  6.77118003e-02 -5.14759794e-02
  6.27156124e-02 -1.21618574e-02  1.75668858e-02 -3.78762819e-02
 -3.98246832e-02 -2.57253591e-02  2.53311358e-02 -3.61068845e-02
 -2.31556576e-02 -9.27493498e-02 -1.47825507e-02  3.14655341e-03
  2.56452523e-02  1.57690775e-02 -2.70829033e-02 -6.37597889e-02
  3.86471972e-02 -7.73148239e-02  3.12786661e-02  2.23171134e-02
 -8.56999122e-03 -4.85717095e-02 -3.61798182e-02  1.63648522e-03
  1.61707308e-02 -6.99889939e-03 -5.18711731e-02 -1.90790314e-02
  4.30933386e-02  1.98659543e-02 -1.26287306e-03 -1.96319818e-02
 -5.04849385e-03  5.16823716e-02  5.03076753e-03  2.31178366e-02
  2.43603878e-04  1.05333567e-01 -7.29547068e-02  2.32017990e-02
 -3.38677429e-02 -8.43357071e-02  1.67592224e-02 -2.62945681e-03
 -7.03678876e-02 -2.12111790e-02 -3.37112769e-02  9.35097337e-02
 -6.48424253e-02  8.23471136e-03 -2.30016699e-03  2.22745091e-02
 -4.75440584e-02  1.63520277e-02  1.95694864e-02  1.96297374e-03
  2.45729517e-02 -9.29153408e-04 -1.12764910e-03  1.53526794e-02
  1.04680873e-01 -1.27797306e-01 -3.35972244e-03  3.22399773e-02
  4.31826338e-02  5.01858816e-02 -2.07837147e-04  2.86541792e-04
 -3.98887582e-02 -6.74812943e-02 -4.15129922e-02  3.65940817e-02
 -3.93773131e-02 -8.05073977e-02  2.38784379e-03  3.32691632e-02
  2.83783004e-02 -3.66698243e-02 -5.62129430e-02  5.01792654e-02
 -2.33405139e-02  1.61499791e-02 -4.29264754e-02  4.30448316e-02
  2.56727841e-02 -1.88454371e-02  6.64583817e-02  1.93248279e-02
  4.67719659e-02  1.45313125e-02  8.05861205e-02 -4.58032498e-03
  3.33893113e-02 -5.94025739e-02 -3.12952250e-02  8.35658703e-03
 -2.74883527e-02  7.64452145e-02 -6.00770488e-02  7.91027918e-02
  6.31458461e-02 -2.81546544e-02 -1.37154991e-02  1.48561904e-02
 -2.09345929e-02 -2.96633113e-02  1.54083921e-02 -3.85329872e-02
 -6.11911193e-02  1.59044396e-02 -9.26953107e-02 -4.97274809e-02
  3.10599357e-02 -7.44235795e-03  3.71270487e-03  5.68280928e-02
 -8.46656598e-03 -3.45869288e-02 -2.54095942e-02  1.97500437e-02
 -2.68470179e-02 -2.08695307e-02 -2.42726207e-02  4.38429043e-03
 -4.33459021e-02  3.42355333e-02 -2.21552942e-02  9.02087241e-02
  5.87272681e-02  2.39975210e-02  2.51395553e-02  1.97111499e-02
 -3.09622400e-02 -2.84835156e-02 -5.12582762e-03 -3.70559306e-03
  9.37485322e-02 -2.53317133e-02 -4.42560278e-02  5.35009168e-02
 -3.65039892e-02 -6.23504445e-03 -3.96688096e-02  4.74160165e-02
 -3.81737426e-02  2.27259193e-03  7.61220511e-03  2.18334813e-02
 -3.55365090e-02  4.50529829e-02 -1.85751971e-02 -6.23184703e-02
  8.72130319e-03 -1.56292552e-03 -3.26789953e-02  1.84043217e-02
  2.71057915e-02 -5.51696820e-03 -9.33860168e-02  2.01406926e-02
  2.42918357e-03 -1.19947810e-02  3.05434968e-03 -4.49000075e-02
 -6.85363784e-02  3.84860411e-02  2.64314283e-02  2.33045071e-02
 -2.70613302e-02 -6.86794892e-02 -7.83752054e-02 -7.87306279e-02
 -1.30088292e-02 -8.76389351e-03  1.63261518e-02 -6.25835061e-02
  9.40966606e-03 -3.11086718e-02  4.25606035e-03 -3.60562578e-02
 -1.73386245e-04 -3.50093395e-02 -7.35225230e-02  1.98400524e-02
 -9.52952728e-02 -8.12650472e-02 -4.27488126e-02  1.17040761e-02
  7.27333175e-03  4.73947376e-02  5.54037467e-02  7.01924637e-02
 -4.04515676e-02 -6.79700300e-02 -7.16285333e-02  1.74667686e-02
  2.04322464e-03 -4.86197881e-02 -2.48414557e-02 -7.26240203e-02
  3.84751633e-02 -5.32855242e-02 -4.15547155e-02 -6.42309487e-02
 -6.75276062e-03 -4.53741178e-02  7.46533647e-03 -7.87081383e-03
 -1.31458358e-03 -3.68053466e-02  2.57662833e-02 -4.46929364e-03
 -2.06748699e-03  1.99163314e-02  4.15304489e-02  2.59832293e-02
 -1.50207477e-02 -1.60634203e-03 -2.11432334e-02  1.77609175e-02
 -5.30025661e-02 -3.36629115e-02  5.41122351e-03  2.10579764e-02
  1.59967486e-02  1.54141337e-02  1.06600942e-02  6.56153113e-02
 -1.21084666e-02 -4.99734655e-02 -1.78743135e-02  2.56294804e-03
 -1.85309369e-02  1.32340577e-03  7.67541025e-03 -1.24680819e-02
  1.53440563e-02  6.35856912e-02 -2.15260573e-02  2.21800692e-02
 -6.31161779e-02  7.11297244e-02  3.12078726e-02  4.75737676e-02
  3.71731981e-03 -1.25175370e-02 -2.27267332e-02 -2.34144367e-03
  1.90492198e-02 -1.26861818e-02  4.57468070e-02 -4.24622856e-02
  4.37927507e-02 -5.12449406e-02  8.09009075e-02 -5.73104853e-03
  2.68225529e-04  4.24808599e-02 -3.77354398e-02  1.68645959e-02
  6.29078150e-02  7.13148480e-03 -4.22078818e-02  2.24268381e-02
  4.79769818e-02  2.53349077e-02  1.27813611e-02  2.56435163e-02
  1.62322652e-02 -1.17148915e-02 -6.30342588e-02  8.70208219e-02
  6.27330169e-02 -6.92561790e-02  1.00028301e-02 -7.49570653e-02
 -5.54122292e-02  1.29838306e-02  1.24240797e-02 -1.31379385e-02]
In [9]:
from facenet_pytorch import MTCNN, InceptionResnetV1, extract_face
from PIL import Image
from sklearn.metrics.pairwise import euclidean_distances

# Function to process images, detect faces, and calculate embeddings
def process_images(folder_path1, file1, folder_path2, file2, face_detector, face_embedder):
    # Read the images
    image1_path = os.path.join(folder_path1, file1)
    image2_path = os.path.join(folder_path2, file2)

    image1 = cv2.imread(image1_path)
    image1_rgb = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

    image2 = cv2.imread(image2_path)
    image2_rgb = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)

    # Detect faces using MTCNN
    faces1, _ = face_detector.detect(image1_rgb)
    faces2, _ = face_detector.detect(image2_rgb)

    # Get labels from folder names
    label1 = os.path.basename(folder_path1)
    label2 = os.path.basename(folder_path2)

    # Display original images with rectangles around detected faces
    plt.subplot(1, 2, 1)
    plt.imshow(Image.fromarray(image1_rgb))
    plt.title(f"Original Image 1\nLabel: {label1}")

    for i, face in enumerate(faces1):
        x, y, w, h = face
        # Draw a rectangle around the detected face
        cv2.rectangle(image1_rgb, (int(x), int(y)), (int(x+w), int(y+h)), (255, 0, 0), 2)

    plt.subplot(1, 2, 2)
    plt.imshow(Image.fromarray(image1_rgb))
    plt.title(f"Detected Faces 1")
    plt.axis('off')

    plt.show()

    plt.subplot(1, 2, 1)
    plt.imshow(Image.fromarray(image2_rgb))
    plt.title(f"Original Image 2\nLabel: {label2}")

    for i, face in enumerate(faces2):
        x, y, w, h = face
        # Draw a rectangle around the detected face
        cv2.rectangle(image2_rgb, (int(x), int(y)), (int(x+w), int(y+h)), (255, 0, 0), 2)

    plt.subplot(1, 2, 2)
    plt.imshow(Image.fromarray(image2_rgb))
    plt.title(f"Detected Faces 2")
    plt.axis('off')

    plt.show()

    # Calculate embeddings for the first image
    embeddings1 = [face_embedder(extract_face(image1_rgb, face).unsqueeze(0)).squeeze().detach().numpy() for face in faces1]

    # Print the embeddings for the first image
    #for i, embeddings in enumerate(embeddings1):
     #   print(f"Embeddings for Detected Face {i + 1} in Image 1:", embeddings)

    # Calculate embeddings for the second image
    embeddings2 = [face_embedder(extract_face(image2_rgb, face).unsqueeze(0)).squeeze().detach().numpy() for face in faces2]

    # Print the embeddings for the second image
   # for i, embeddings in enumerate(embeddings2):
    #    print(f"Embeddings for Detected Face {i + 1} in Image 2:", embeddings)

    # Calculate distance between the two embeddings
    distance = euclidean_distances(embeddings1[0].reshape(1, -1), embeddings2[0].reshape(1, -1))[0][0]

    # Display the distance between the two embeddings
    print(f"Distance between embeddings: {distance:.4f}")

# Directory paths
train_directory = 'working/train/'

# Randomly pick two folders
selected_folders = random.sample(os.listdir(train_directory), 2)

# Randomly pick one file from each selected folder
file1 = random.choice(os.listdir(os.path.join(train_directory, selected_folders[0])))
file2 = random.choice(os.listdir(os.path.join(train_directory, selected_folders[1])))

# Initialize MTCNN for face detection
face_detector = MTCNN(keep_all=True)

# Initialize InceptionResnetV1 for face embedding
face_embedder = InceptionResnetV1(pretrained='vggface2').eval()

# Process images, detect faces, calculate embeddings, and display results
process_images(
    os.path.join(train_directory, selected_folders[0]),
    file1,
    os.path.join(train_directory, selected_folders[1]),
    file2,
    face_detector,
    face_embedder
)
Distance between embeddings: 0.3837
In [10]:
from facenet_pytorch import MTCNN, InceptionResnetV1, extract_face
from PIL import Image
from sklearn.metrics.pairwise import euclidean_distances

# Function to process images, detect faces, and calculate embeddings
def process_images(folder_path, face_detector, face_embedder):
    # Randomly pick two files from the selected folder
    files = random.sample(os.listdir(folder_path), 2)
    
    # Read the images
    image1_path = os.path.join(folder_path, files[0])
    image2_path = os.path.join(folder_path, files[1])

    image1 = cv2.imread(image1_path)
    image1_rgb = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

    image2 = cv2.imread(image2_path)
    image2_rgb = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)

    # Detect faces using MTCNN
    faces1, _ = face_detector.detect(image1_rgb)
    faces2, _ = face_detector.detect(image2_rgb)

    # Get the label from the folder name
    label = os.path.basename(folder_path)

    # Display original images with rectangles around detected faces
    plt.subplot(1, 2, 1)
    plt.imshow(Image.fromarray(image1_rgb))
    plt.title(f"Original Image 1\nLabel: {label}")

    for i, face in enumerate(faces1):
        x, y, w, h = face
        # Draw a rectangle around the detected face
        cv2.rectangle(image1_rgb, (int(x), int(y)), (int(x+w), int(y+h)), (255, 0, 0), 2)

    plt.subplot(1, 2, 2)
    plt.imshow(Image.fromarray(image1_rgb))
    plt.title(f"Detected Faces 1")
    plt.axis('off')

    plt.show()

    plt.subplot(1, 2, 1)
    plt.imshow(Image.fromarray(image2_rgb))
    plt.title(f"Original Image 2\nLabel: {label}")

    for i, face in enumerate(faces2):
        x, y, w, h = face
        # Draw a rectangle around the detected face
        cv2.rectangle(image2_rgb, (int(x), int(y)), (int(x+w), int(y+h)), (255, 0, 0), 2)

    plt.subplot(1, 2, 2)
    plt.imshow(Image.fromarray(image2_rgb))
    plt.title(f"Detected Faces 2")
    plt.axis('off')

    plt.show()

    # Calculate embeddings for the first image
    embeddings1 = [face_embedder(extract_face(image1_rgb, face).unsqueeze(0)).squeeze().detach().numpy() for face in faces1]

    # Print the embeddings for the first image
    #for i, embeddings in enumerate(embeddings1):
     #   print(f"Embeddings for Detected Face {i + 1} in Image 1:", embeddings)

    # Calculate embeddings for the second image
    embeddings2 = [face_embedder(extract_face(image2_rgb, face).unsqueeze(0)).squeeze().detach().numpy() for face in faces2]

    # Print the embeddings for the second image
   # for i, embeddings in enumerate(embeddings2):
    #    print(f"Embeddings for Detected Face {i + 1} in Image 2:", embeddings)

    # Calculate distance between the two embeddings
    distance = euclidean_distances(embeddings1[0].reshape(1, -1), embeddings2[0].reshape(1, -1))[0][0]

    # Display the distance between the two embeddings
    print(f"Distance between embeddings: {distance:.4f}")

# Directory paths
train_directory = 'working/train/'

# Randomly pick one folder
selected_folder = random.choice(os.listdir(train_directory))
selected_folder_path = os.path.join(train_directory, selected_folder)

# Initialize MTCNN for face detection
face_detector = MTCNN(keep_all=True)

# Initialize InceptionResnetV1 for face embedding
face_embedder = InceptionResnetV1(pretrained='vggface2').eval()

# Process images, detect faces, calculate embeddings, and display results
process_images(selected_folder_path, face_detector, face_embedder)
Distance between embeddings: 0.3055
In [11]:
import os
import shutil

# Set the directory path
directory_path = 'working/train_detected_faces'

# Create the directory if it doesn't exist
if not os.path.exists(directory_path):
    os.makedirs(directory_path)
    print(f"Directory '{directory_path}' created.")

# Remove all files and subdirectories in the directory
for item in os.listdir(directory_path):
    item_path = os.path.join(directory_path, item)
    if os.path.isfile(item_path) or os.path.islink(item_path):
        os.unlink(item_path)
    elif os.path.isdir(item_path):
        shutil.rmtree(item_path)

print("Contents of train_detected_face cleared.")
Directory 'working/train_detected_faces' created.
Contents of train_detected_face cleared.
In [12]:
import os
import cv2
import numpy as np

data_directory = 'C:\\Users\\Ifham.Khan\\Desktop\\Final Year Project\\105_classes_pins_dataset'
output_directory = 'C:\\Users\\Ifham.Khan\\Desktop\\Final Year Project\\working\\train_detected_faces'

def detect_faces_and_save(image_paths, output_directory):
    processed_images = []
    images_without_faces = 0

    for i, image_path in enumerate(image_paths):
        img = cv2.imread(image_path)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # Use OpenCV's deep learning-based face detector
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)

        if len(faces) > 0:
            # Assuming only one face is detected, adjust the dimensions
            x, y, w, h = faces[0]
            face_roi = img[y:y+h, x:x+w]
            resized_face = cv2.resize(face_roi, (224, 224))

            # Get the original folder name
            folder_name = image_path.split('\\')[-2]
            # Create the output folder if it doesn't exist
            output_folder = os.path.join(output_directory, folder_name)
            os.makedirs(output_folder, exist_ok=True)

            # Save the detected face with the same folder structure
            output_path = os.path.join(output_folder, f"detected_face_{i}.jpg")
            cv2.imwrite(output_path, resized_face)

            processed_images.append(resized_face)
        else:
            images_without_faces += 1

        if i % 50 == 0:
            print(f"{i}/{len(image_paths)} images processed", end='\r', flush=True)

    print(f"\nImages without faces detected/Total images: {images_without_faces}/{len(image_paths)}")
    return np.array(processed_images)

# Create output directory if it doesn't exist
os.makedirs(output_directory, exist_ok=True)

# Get list of all image paths
all_image_paths = []
for folder_name in os.listdir(data_directory):
    folder_path = os.path.join(data_directory, folder_name)
    if os.path.isdir(folder_path):
        image_paths = [os.path.join(folder_path, image_name) for image_name in os.listdir(folder_path)]
        all_image_paths.extend(image_paths)

X_all_processed = detect_faces_and_save(all_image_paths, output_directory)

print("Face detection and saving complete.")
17500/17534 images processed
Images without faces detected/Total images: 4069/17534
Face detection and saving complete.
In [13]:
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np

# Replace this with the path to your detected faces directory
output_directory = 'working/train_detected_faces'

# Get a list of subdirectories (folders) in the output directory
folders = [f for f in os.listdir(output_directory) if os.path.isdir(os.path.join(output_directory, f))]

# Randomly pick five folders (allowing replacement if needed)
selected_folders = np.random.choice(folders, size=min(5, len(folders)), replace=True)

# Set up the subplot
fig, axes = plt.subplots(nrows=len(selected_folders), ncols=5, figsize=(15, 15))

for i, folder_name in enumerate(selected_folders):
    folder_path = os.path.join(output_directory, folder_name)
    image_paths = [os.path.join(folder_path, image_name) for image_name in os.listdir(folder_path)[:5]]

    for j, image_path in enumerate(image_paths):
        # Read the image using OpenCV
        img = cv2.imread(image_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        # Display the image
        axes[i, j].imshow(img)
        axes[i, j].set_title(f'{folder_name} - {j+1}')
        axes[i, j].axis('off')

plt.tight_layout()
plt.show()
In [ ]:
#!pip install facenet-pytorch
In [14]:
from facenet_pytorch import InceptionResnetV1
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.metrics.pairwise import euclidean_distances
import torch

# Function to process images and calculate embeddings
def process_images(face1_path, face2_path, face_embedder):
    # Read the pre-detected face images
    face1 = cv2.imread(face1_path)
    face1_rgb = cv2.cvtColor(face1, cv2.COLOR_BGR2RGB)

    face2 = cv2.imread(face2_path)
    face2_rgb = cv2.cvtColor(face2, cv2.COLOR_BGR2RGB)

    # Get labels from folder names
    label1 = os.path.basename(os.path.dirname(face1_path))
    label2 = os.path.basename(os.path.dirname(face2_path))

    # Display original face images
    plt.subplot(1, 2, 1)
    plt.imshow(Image.fromarray(face1_rgb))
    plt.title(f"Original Face 1\nLabel: {label1}")
    plt.axis('off')

    plt.subplot(1, 2, 2)
    plt.imshow(Image.fromarray(face2_rgb))
    plt.title(f"Original Face 2\nLabel: {label2}")
    plt.axis('off')

    plt.show()

    # Convert NumPy arrays to PyTorch tensors
    face1_tensor = torch.from_numpy(face1_rgb.transpose(2, 0, 1)).unsqueeze(0).float() / 255.0
    face2_tensor = torch.from_numpy(face2_rgb.transpose(2, 0, 1)).unsqueeze(0).float() / 255.0

    # Calculate embeddings for the first face
    embeddings1 = face_embedder(face1_tensor).detach().numpy()

    # Print the embeddings for the first face
    # print("Embeddings for Face 1:", embeddings1)

    # Calculate embeddings for the second face
    embeddings2 = face_embedder(face2_tensor).detach().numpy()

    # Print the embeddings for the second face
    # print("Embeddings for Face 2:", embeddings2)

    # Calculate distance between the two embeddings
    distance = euclidean_distances(embeddings1.reshape(1, -1), embeddings2.reshape(1, -1))[0][0]

    # Display the distance between the two embeddings
    print(f"Distance between embeddings: {distance:.4f}")

# Directory paths
train_detected_faces_directory = 'working/train_detected_faces'

# Get a list of folders in the train_detected_faces_directory
folders = [f for f in os.listdir(train_detected_faces_directory) if os.path.isdir(os.path.join(train_detected_faces_directory, f))]

# Ensure there are at least two folders
if len(folders) >= 2:
    # Randomly pick two folders
    selected_folders = random.sample(folders, 2)

    # Randomly pick one file from each selected folder
    file1 = random.choice(os.listdir(os.path.join(train_detected_faces_directory, selected_folders[0])))
    file2 = random.choice(os.listdir(os.path.join(train_detected_faces_directory, selected_folders[1])))

    # Full paths for the selected faces
    face1_path = os.path.join(train_detected_faces_directory, selected_folders[0], file1)
    face2_path = os.path.join(train_detected_faces_directory, selected_folders[1], file2)

    # Initialize InceptionResnetV1 for face embedding
    face_embedder = InceptionResnetV1(pretrained='vggface2').eval()

    # Process face images, calculate embeddings, and display results
    process_images(face1_path, face2_path, face_embedder)
else:
    print("Not enough folders in the specified directory.")
Distance between embeddings: 1.1345
In [15]:
import os
import random
from facenet_pytorch import InceptionResnetV1
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import numpy as np
from sklearn.metrics.pairwise import euclidean_distances
import torch

# Function to process images and calculate embeddings
def process_images(folder_path, face1_file, face2_file, face_embedder):
    # Read the pre-detected face images
    face1_path = os.path.join(folder_path, face1_file)
    face2_path = os.path.join(folder_path, face2_file)

    face1 = cv2.imread(face1_path)
    face1_rgb = cv2.cvtColor(face1, cv2.COLOR_BGR2RGB)

    face2 = cv2.imread(face2_path)
    face2_rgb = cv2.cvtColor(face2, cv2.COLOR_BGR2RGB)

    # Display original face images
    plt.subplot(1, 2, 1)
    plt.imshow(Image.fromarray(face1_rgb))
    plt.title(f"Original Face 1\nFolder: {os.path.basename(folder_path)}")
    plt.axis('off')

    plt.subplot(1, 2, 2)
    plt.imshow(Image.fromarray(face2_rgb))
    plt.title(f"Original Face 2\nFolder: {os.path.basename(folder_path)}")
    plt.axis('off')

    plt.show()

    # Convert NumPy arrays to PyTorch tensors
    face1_tensor = torch.from_numpy(face1_rgb.transpose(2, 0, 1)).unsqueeze(0).float() / 255.0
    face2_tensor = torch.from_numpy(face2_rgb.transpose(2, 0, 1)).unsqueeze(0).float() / 255.0

    # Calculate embeddings for the first face
    embeddings1 = face_embedder(face1_tensor).detach().numpy()

    # Print the embeddings for the first face
    # print("Embeddings for Face 1:", embeddings1)

    # Calculate embeddings for the second face
    embeddings2 = face_embedder(face2_tensor).detach().numpy()

    # Print the embeddings for the second face
    # print("Embeddings for Face 2:", embeddings2)

    # Calculate distance between the two embeddings
    distance = euclidean_distances(embeddings1.reshape(1, -1), embeddings2.reshape(1, -1))[0][0]

    # Display the distance between the two embeddings
    print(f"Distance between embeddings: {distance:.4f}")

# Directory paths
train_detected_faces_directory = 'working/train_detected_faces'

# Randomly pick one folder
selected_folder = random.choice(os.listdir(train_detected_faces_directory))
folder_path = os.path.join(train_detected_faces_directory, selected_folder)

# Randomly pick two different files from the selected folder
files = random.sample(os.listdir(folder_path), 2)
face1_file, face2_file = files

# Initialize InceptionResnetV1 for face embedding
face_embedder = InceptionResnetV1(pretrained='vggface2').eval()

# Process face images, calculate embeddings, and display results
process_images(folder_path, face1_file, face2_file, face_embedder)
Distance between embeddings: 0.8947
In [16]:
import os

output_directory = 'working/extracted_embeddings'
os.makedirs(output_directory, exist_ok=True)
print(f"Directory '{output_directory}' created.")
Directory 'working/extracted_embeddings' created.
In [17]:
import shutil
import os

folder_path = 'working/extracted_embeddings'

# Check if the folder exists before deleting
if os.path.exists(folder_path):
    shutil.rmtree(folder_path)
    print(f"The folder {folder_path} has been deleted.")
else:
    print(f"The folder {folder_path} does not exist.")
The folder working/extracted_embeddings has been deleted.
In [24]:
import torch
from facenet_pytorch import InceptionResnetV1
from PIL import Image
from torchvision import transforms
from tqdm import tqdm

# Function to extract embeddings from a single folder with data augmentation
def extract_embeddings_from_folder(folder_path, face_embedder, device, output_directory):
    embeddings = {}
    data_transform = transforms.Compose([
        transforms.RandomHorizontalFlip(),
        transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
        transforms.RandomRotation(10),
        transforms.ToTensor()
    ])

    label = os.path.basename(folder_path)  # Get the label from the folder name

    for image_name in tqdm(os.listdir(folder_path), desc=f"Processing {folder_path}"):
        image_path = os.path.join(folder_path, image_name)
        try:
            img = Image.open(image_path)

            # Convert image to tensor
            img_tensor = transforms.ToTensor()(img).unsqueeze(0).float().to(device)

            # Calculate embedding
            embedding = face_embedder(img_tensor).squeeze().detach().cpu().numpy()
            embeddings[image_name] = embedding

            # Save the embedding for the original image
            output_emb_path = os.path.join(output_directory, f"{label}_{os.path.splitext(image_name)[0]}_embedding.npy")
            np.save(output_emb_path, embedding)

            # Apply data augmentation
            augmented_img = data_transform(img)

            # Convert augmented image to tensor
            img_tensor_augmented = augmented_img.unsqueeze(0).float().to(device)

            # Calculate embedding for the augmented image
            embedding_augmented = face_embedder(img_tensor_augmented).squeeze().detach().cpu().numpy()
            embeddings[f"{os.path.splitext(image_name)[0]}_augmented_embedding.npy"] = embedding_augmented

            # Save the embedding for the augmented image
            output_emb_path_augmented = os.path.join(output_directory, f"{label}_{os.path.splitext(image_name)[0]}_augmented_embedding.npy")
            np.save(output_emb_path_augmented, embedding_augmented)

        except Exception as e:
            print(f"Error processing {image_name}: {str(e)}")

    return label, embeddings

# Directory paths
input_directory = 'working/train_detected_faces'
output_directory = 'working/extracted_embeddings'

# Initialize InceptionResnetV1 for face embedding
device = 'cpu'
#device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
face_embedder = InceptionResnetV1(pretrained='vggface2').eval().to(device)

# Loop through each folder in the input directory
for folder_name in os.listdir(input_directory):
    folder_path = os.path.join(input_directory, folder_name)

    # Create subfolder in output_directory for the current label
    label_output_directory = os.path.join(output_directory, folder_name)
    os.makedirs(label_output_directory, exist_ok=True)

    # Extract embeddings from the current folder with data augmentation
    label, embeddings = extract_embeddings_from_folder(folder_path, face_embedder, device, label_output_directory)

print("Embeddings extraction complete.")
Processing working/train_detected_faces\pins_Adriana Lima: 100%|█████████████████████| 186/186 [02:04<00:00,  1.50it/s]
Processing working/train_detected_faces\pins_Alex Lawther: 100%|███████████████████████| 86/86 [00:54<00:00,  1.58it/s]
Processing working/train_detected_faces\pins_Alexandra Daddario: 100%|███████████████| 206/206 [02:10<00:00,  1.58it/s]
Processing working/train_detected_faces\pins_Alvaro Morte: 100%|███████████████████████| 94/94 [01:01<00:00,  1.54it/s]
Processing working/train_detected_faces\pins_alycia dabnem carey: 100%|██████████████| 157/157 [01:40<00:00,  1.57it/s]
Processing working/train_detected_faces\pins_Amanda Crew: 100%|██████████████████████| 110/110 [01:09<00:00,  1.59it/s]
Processing working/train_detected_faces\pins_amber heard: 100%|██████████████████████| 166/166 [01:49<00:00,  1.52it/s]
Processing working/train_detected_faces\pins_Andy Samberg: 100%|█████████████████████| 167/167 [01:46<00:00,  1.56it/s]
Processing working/train_detected_faces\pins_Anne Hathaway: 100%|████████████████████| 157/157 [01:41<00:00,  1.54it/s]
Processing working/train_detected_faces\pins_Anthony Mackie: 100%|█████████████████████| 78/78 [00:50<00:00,  1.55it/s]
Processing working/train_detected_faces\pins_Avril Lavigne: 100%|████████████████████| 108/108 [01:10<00:00,  1.53it/s]
Processing working/train_detected_faces\pins_barack obama: 100%|███████████████████████| 87/87 [00:55<00:00,  1.58it/s]
Processing working/train_detected_faces\pins_barbara palvin: 100%|███████████████████| 144/144 [01:31<00:00,  1.57it/s]
Processing working/train_detected_faces\pins_Ben Affleck: 100%|████████████████████████| 98/98 [01:01<00:00,  1.58it/s]
Processing working/train_detected_faces\pins_Bill Gates: 100%|█████████████████████████| 69/69 [00:46<00:00,  1.47it/s]
Processing working/train_detected_faces\pins_Bobby Morley: 100%|███████████████████████| 69/69 [00:44<00:00,  1.56it/s]
Processing working/train_detected_faces\pins_Brenton Thwaites: 100%|█████████████████| 159/159 [01:43<00:00,  1.54it/s]
Processing working/train_detected_faces\pins_Brian J. Smith: 100%|█████████████████████| 78/78 [00:50<00:00,  1.55it/s]
Processing working/train_detected_faces\pins_Brie Larson: 100%|██████████████████████| 131/131 [01:24<00:00,  1.54it/s]
Processing working/train_detected_faces\pins_camila mendes: 100%|████████████████████| 130/130 [01:26<00:00,  1.50it/s]
Processing working/train_detected_faces\pins_Chris Evans: 100%|██████████████████████| 111/111 [01:12<00:00,  1.54it/s]
Processing working/train_detected_faces\pins_Chris Hemsworth: 100%|██████████████████| 133/133 [01:24<00:00,  1.57it/s]
Processing working/train_detected_faces\pins_Chris Pratt: 100%|██████████████████████| 158/158 [01:42<00:00,  1.53it/s]
Processing working/train_detected_faces\pins_Christian Bale: 100%|███████████████████| 129/129 [01:23<00:00,  1.54it/s]
Processing working/train_detected_faces\pins_Cristiano Ronaldo: 100%|██████████████████| 76/76 [00:49<00:00,  1.55it/s]
Processing working/train_detected_faces\pins_Danielle Panabaker: 100%|███████████████| 149/149 [01:41<00:00,  1.47it/s]
Processing working/train_detected_faces\pins_Dominic Purcell: 100%|██████████████████| 108/108 [01:08<00:00,  1.57it/s]
Processing working/train_detected_faces\pins_Dwayne Johnson: 100%|█████████████████████| 78/78 [00:52<00:00,  1.48it/s]
Processing working/train_detected_faces\pins_Eliza Taylor: 100%|█████████████████████| 130/130 [01:24<00:00,  1.55it/s]
Processing working/train_detected_faces\pins_Elizabeth Lail: 100%|███████████████████| 129/129 [01:23<00:00,  1.55it/s]
Processing working/train_detected_faces\pins_elizabeth olsen: 100%|██████████████████| 194/194 [02:04<00:00,  1.56it/s]
Processing working/train_detected_faces\pins_ellen page: 100%|███████████████████████| 157/157 [01:40<00:00,  1.56it/s]
Processing working/train_detected_faces\pins_elon musk: 100%|████████████████████████| 103/103 [01:05<00:00,  1.57it/s]
Processing working/train_detected_faces\pins_Emilia Clarke: 100%|████████████████████| 159/159 [01:41<00:00,  1.57it/s]
Processing working/train_detected_faces\pins_Emma Stone: 100%|███████████████████████| 100/100 [01:05<00:00,  1.53it/s]
Processing working/train_detected_faces\pins_Emma Watson: 100%|██████████████████████| 162/162 [01:44<00:00,  1.55it/s]
Processing working/train_detected_faces\pins_gal gadot: 100%|████████████████████████| 134/134 [01:24<00:00,  1.59it/s]
Processing working/train_detected_faces\pins_grant gustin: 100%|█████████████████████| 148/148 [01:35<00:00,  1.56it/s]
Processing working/train_detected_faces\pins_Gwyneth Paltrow: 100%|██████████████████| 160/160 [01:42<00:00,  1.56it/s]
Processing working/train_detected_faces\pins_Henry Cavil: 100%|██████████████████████| 144/144 [01:52<00:00,  1.27it/s]
Processing working/train_detected_faces\pins_Hugh Jackman: 100%|█████████████████████| 144/144 [01:56<00:00,  1.24it/s]
Processing working/train_detected_faces\pins_Inbar Lavi: 100%|███████████████████████| 110/110 [00:53<00:00,  2.07it/s]
Processing working/train_detected_faces\pins_Irina Shayk: 100%|██████████████████████| 137/137 [00:50<00:00,  2.73it/s]
Processing working/train_detected_faces\pins_Jake Mcdorman: 100%|████████████████████| 147/147 [00:53<00:00,  2.73it/s]
Processing working/train_detected_faces\pins_Jason Momoa: 100%|██████████████████████| 143/143 [00:51<00:00,  2.80it/s]
Processing working/train_detected_faces\pins_jeff bezos: 100%|█████████████████████████| 77/77 [00:27<00:00,  2.80it/s]
Processing working/train_detected_faces\pins_Jennifer Lawrence: 100%|████████████████| 153/153 [00:54<00:00,  2.81it/s]
Processing working/train_detected_faces\pins_Jeremy Renner: 100%|████████████████████| 129/129 [00:51<00:00,  2.49it/s]
Processing working/train_detected_faces\pins_Jessica Barden: 100%|███████████████████| 114/114 [00:47<00:00,  2.38it/s]
Processing working/train_detected_faces\pins_Jimmy Fallon: 100%|███████████████████████| 94/94 [00:34<00:00,  2.73it/s]
Processing working/train_detected_faces\pins_Johnny Depp: 100%|██████████████████████| 142/142 [00:47<00:00,  3.01it/s]
Processing working/train_detected_faces\pins_Josh Radnor: 100%|██████████████████████| 101/101 [00:34<00:00,  2.96it/s]
Processing working/train_detected_faces\pins_Katharine Mcphee: 100%|█████████████████| 159/159 [00:53<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_Katherine Langford: 100%|███████████████| 196/196 [01:06<00:00,  2.96it/s]
Processing working/train_detected_faces\pins_Keanu Reeves: 100%|█████████████████████| 128/128 [00:42<00:00,  3.00it/s]
Processing working/train_detected_faces\pins_kiernen shipka: 100%|███████████████████| 170/170 [00:56<00:00,  3.00it/s]
Processing working/train_detected_faces\pins_Krysten Ritter: 100%|███████████████████| 142/142 [00:47<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_Leonardo DiCaprio: 100%|████████████████| 173/173 [00:57<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_Lili Reinhart: 100%|████████████████████| 108/108 [00:36<00:00,  2.97it/s]
Processing working/train_detected_faces\pins_Lindsey Morgan: 100%|███████████████████| 133/133 [00:45<00:00,  2.95it/s]
Processing working/train_detected_faces\pins_Lionel Messi: 100%|███████████████████████| 66/66 [00:22<00:00,  2.97it/s]
Processing working/train_detected_faces\pins_Logan Lerman: 100%|█████████████████████| 174/174 [00:58<00:00,  2.96it/s]
Processing working/train_detected_faces\pins_Madelaine Petsch: 100%|█████████████████| 134/134 [00:45<00:00,  2.95it/s]
Processing working/train_detected_faces\pins_Maisie Williams: 100%|██████████████████| 154/154 [00:51<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_margot robbie: 100%|████████████████████| 148/148 [00:49<00:00,  3.01it/s]
Processing working/train_detected_faces\pins_Maria Pedraza: 100%|██████████████████████| 78/78 [00:26<00:00,  3.00it/s]
Processing working/train_detected_faces\pins_Marie Avgeropoulos: 100%|███████████████| 136/136 [00:46<00:00,  2.92it/s]
Processing working/train_detected_faces\pins_Mark Ruffalo: 100%|█████████████████████| 135/135 [00:47<00:00,  2.83it/s]
Processing working/train_detected_faces\pins_Mark Zuckerberg: 100%|████████████████████| 81/81 [00:27<00:00,  2.96it/s]
Processing working/train_detected_faces\pins_Megan Fox: 100%|████████████████████████| 173/173 [00:59<00:00,  2.93it/s]
Processing working/train_detected_faces\pins_melissa fumero: 100%|███████████████████| 137/137 [00:47<00:00,  2.87it/s]
Processing working/train_detected_faces\pins_Miley Cyrus: 100%|██████████████████████| 101/101 [00:33<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_Millie Bobby Brown: 100%|███████████████| 139/139 [00:46<00:00,  2.98it/s]
Processing working/train_detected_faces\pins_Morena Baccarin: 100%|██████████████████| 161/161 [00:53<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_Morgan Freeman: 100%|█████████████████████| 57/57 [00:19<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_Nadia Hilker: 100%|███████████████████████| 94/94 [00:32<00:00,  2.92it/s]
Processing working/train_detected_faces\pins_Natalie Dormer: 100%|███████████████████| 144/144 [00:48<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_Natalie Portman: 100%|██████████████████| 124/124 [00:41<00:00,  3.00it/s]
Processing working/train_detected_faces\pins_Neil Patrick Harris: 100%|████████████████| 98/98 [00:33<00:00,  2.96it/s]
Processing working/train_detected_faces\pins_Pedro Alonso: 100%|█████████████████████| 100/100 [00:33<00:00,  2.97it/s]
Processing working/train_detected_faces\pins_Penn Badgley: 100%|█████████████████████| 142/142 [00:47<00:00,  3.00it/s]
Processing working/train_detected_faces\pins_Rami Malek: 100%|███████████████████████| 124/124 [00:41<00:00,  2.96it/s]
Processing working/train_detected_faces\pins_Rebecca Ferguson: 100%|█████████████████| 155/155 [01:12<00:00,  2.13it/s]
Processing working/train_detected_faces\pins_Richard Harmon: 100%|█████████████████████| 98/98 [00:40<00:00,  2.41it/s]
Processing working/train_detected_faces\pins_Rihanna: 100%|████████████████████████████| 89/89 [00:30<00:00,  2.91it/s]
Processing working/train_detected_faces\pins_Robert De Niro: 100%|███████████████████| 115/115 [00:38<00:00,  2.95it/s]
Processing working/train_detected_faces\pins_Robert Downey Jr: 100%|█████████████████| 167/167 [00:56<00:00,  2.97it/s]
Processing working/train_detected_faces\pins_Sarah Wayne Callies: 100%|██████████████| 133/133 [00:44<00:00,  2.98it/s]
Processing working/train_detected_faces\pins_scarlett johansson: 100%|███████████████| 160/160 [00:54<00:00,  2.96it/s]
Processing working/train_detected_faces\pins_Selena Gomez: 100%|█████████████████████| 125/125 [00:43<00:00,  2.89it/s]
Processing working/train_detected_faces\pins_Shakira Isabel Mebarak: 100%|███████████| 126/126 [00:42<00:00,  2.94it/s]
Processing working/train_detected_faces\pins_Sophie Turner: 100%|████████████████████| 159/159 [00:53<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_Stephen Amell: 100%|████████████████████| 129/129 [00:43<00:00,  2.97it/s]
Processing working/train_detected_faces\pins_Taylor Swift: 100%|███████████████████████| 43/43 [00:14<00:00,  3.04it/s]
Processing working/train_detected_faces\pins_Tom Cruise: 100%|███████████████████████| 151/151 [00:50<00:00,  3.00it/s]
Processing working/train_detected_faces\pins_tom ellis: 100%|████████████████████████| 118/118 [00:39<00:00,  2.97it/s]
Processing working/train_detected_faces\pins_Tom Hardy: 100%|████████████████████████| 121/121 [00:40<00:00,  3.00it/s]
Processing working/train_detected_faces\pins_Tom Hiddleston: 100%|███████████████████| 137/137 [00:46<00:00,  2.97it/s]
Processing working/train_detected_faces\pins_Tom Holland: 100%|██████████████████████| 151/151 [00:50<00:00,  2.96it/s]
Processing working/train_detected_faces\pins_Tuppence Middleton: 100%|███████████████| 111/111 [00:36<00:00,  3.01it/s]
Processing working/train_detected_faces\pins_Ursula Corbero: 100%|█████████████████████| 86/86 [00:29<00:00,  2.95it/s]
Processing working/train_detected_faces\pins_Wentworth Miller: 100%|█████████████████| 137/137 [00:45<00:00,  2.98it/s]
Processing working/train_detected_faces\pins_Zac Efron: 100%|████████████████████████| 146/146 [00:48<00:00,  2.99it/s]
Processing working/train_detected_faces\pins_Zendaya: 100%|██████████████████████████| 105/105 [00:35<00:00,  2.98it/s]
Processing working/train_detected_faces\pins_Zoe Saldana: 100%|██████████████████████| 161/161 [00:54<00:00,  2.94it/s]
Embeddings extraction complete.

In [20]:
import os

# Directory path
output_directory = 'working/extracted_embeddings'

# Get folder names and file counts
folders_and_counts = [(folder, len(os.listdir(os.path.join(output_directory, folder)))) for folder in os.listdir(output_directory)]

# Print results
for folder, count in folders_and_counts:
    print(f"Folder: {folder}, Number of Files: {count}")
Folder: pins_Adriana Lima, Number of Files: 372
Folder: pins_Alex Lawther, Number of Files: 172
Folder: pins_Alexandra Daddario, Number of Files: 412
Folder: pins_Alvaro Morte, Number of Files: 188
Folder: pins_alycia dabnem carey, Number of Files: 314
Folder: pins_Amanda Crew, Number of Files: 220
Folder: pins_amber heard, Number of Files: 332
Folder: pins_Andy Samberg, Number of Files: 334
Folder: pins_Anne Hathaway, Number of Files: 314
Folder: pins_Anthony Mackie, Number of Files: 156
Folder: pins_Avril Lavigne, Number of Files: 216
Folder: pins_barack obama, Number of Files: 174
Folder: pins_barbara palvin, Number of Files: 288
Folder: pins_Ben Affleck, Number of Files: 196
Folder: pins_Bill Gates, Number of Files: 138
Folder: pins_Bobby Morley, Number of Files: 138
Folder: pins_Brenton Thwaites, Number of Files: 318
Folder: pins_Brian J. Smith, Number of Files: 156
Folder: pins_Brie Larson, Number of Files: 262
Folder: pins_camila mendes, Number of Files: 260
Folder: pins_Chris Evans, Number of Files: 222
Folder: pins_Chris Hemsworth, Number of Files: 266
Folder: pins_Chris Pratt, Number of Files: 316
Folder: pins_Christian Bale, Number of Files: 258
Folder: pins_Cristiano Ronaldo, Number of Files: 152
Folder: pins_Danielle Panabaker, Number of Files: 298
Folder: pins_Dominic Purcell, Number of Files: 216
Folder: pins_Dwayne Johnson, Number of Files: 156
Folder: pins_Eliza Taylor, Number of Files: 260
Folder: pins_Elizabeth Lail, Number of Files: 258
Folder: pins_elizabeth olsen, Number of Files: 388
Folder: pins_ellen page, Number of Files: 314
Folder: pins_elon musk, Number of Files: 206
Folder: pins_Emilia Clarke, Number of Files: 318
Folder: pins_Emma Stone, Number of Files: 200
Folder: pins_Emma Watson, Number of Files: 324
Folder: pins_gal gadot, Number of Files: 268
Folder: pins_grant gustin, Number of Files: 296
Folder: pins_Gwyneth Paltrow, Number of Files: 320
Folder: pins_Henry Cavil, Number of Files: 288
Folder: pins_Hugh Jackman, Number of Files: 288
Folder: pins_Inbar Lavi, Number of Files: 220
Folder: pins_Irina Shayk, Number of Files: 274
Folder: pins_Jake Mcdorman, Number of Files: 294
Folder: pins_Jason Momoa, Number of Files: 286
Folder: pins_jeff bezos, Number of Files: 154
Folder: pins_Jennifer Lawrence, Number of Files: 306
Folder: pins_Jeremy Renner, Number of Files: 258
Folder: pins_Jessica Barden, Number of Files: 228
Folder: pins_Jimmy Fallon, Number of Files: 188
Folder: pins_Johnny Depp, Number of Files: 284
Folder: pins_Josh Radnor, Number of Files: 202
Folder: pins_Katharine Mcphee, Number of Files: 318
Folder: pins_Katherine Langford, Number of Files: 392
Folder: pins_Keanu Reeves, Number of Files: 256
Folder: pins_kiernen shipka, Number of Files: 340
Folder: pins_Krysten Ritter, Number of Files: 284
Folder: pins_Leonardo DiCaprio, Number of Files: 346
Folder: pins_Lili Reinhart, Number of Files: 216
Folder: pins_Lindsey Morgan, Number of Files: 266
Folder: pins_Lionel Messi, Number of Files: 132
Folder: pins_Logan Lerman, Number of Files: 348
Folder: pins_Madelaine Petsch, Number of Files: 268
Folder: pins_Maisie Williams, Number of Files: 308
Folder: pins_margot robbie, Number of Files: 296
Folder: pins_Maria Pedraza, Number of Files: 156
Folder: pins_Marie Avgeropoulos, Number of Files: 272
Folder: pins_Mark Ruffalo, Number of Files: 270
Folder: pins_Mark Zuckerberg, Number of Files: 162
Folder: pins_Megan Fox, Number of Files: 346
Folder: pins_melissa fumero, Number of Files: 274
Folder: pins_Miley Cyrus, Number of Files: 202
Folder: pins_Millie Bobby Brown, Number of Files: 278
Folder: pins_Morena Baccarin, Number of Files: 322
Folder: pins_Morgan Freeman, Number of Files: 114
Folder: pins_Nadia Hilker, Number of Files: 188
Folder: pins_Natalie Dormer, Number of Files: 288
Folder: pins_Natalie Portman, Number of Files: 248
Folder: pins_Neil Patrick Harris, Number of Files: 196
Folder: pins_Pedro Alonso, Number of Files: 200
Folder: pins_Penn Badgley, Number of Files: 284
Folder: pins_Rami Malek, Number of Files: 248
Folder: pins_Rebecca Ferguson, Number of Files: 310
Folder: pins_Richard Harmon, Number of Files: 196
Folder: pins_Rihanna, Number of Files: 178
Folder: pins_Robert De Niro, Number of Files: 230
Folder: pins_Robert Downey Jr, Number of Files: 334
Folder: pins_Sarah Wayne Callies, Number of Files: 266
Folder: pins_scarlett johansson, Number of Files: 320
Folder: pins_Selena Gomez, Number of Files: 250
Folder: pins_Shakira Isabel Mebarak, Number of Files: 252
Folder: pins_Sophie Turner, Number of Files: 318
Folder: pins_Stephen Amell, Number of Files: 258
Folder: pins_Taylor Swift, Number of Files: 86
Folder: pins_Tom Cruise, Number of Files: 302
Folder: pins_tom ellis, Number of Files: 236
Folder: pins_Tom Hardy, Number of Files: 242
Folder: pins_Tom Hiddleston, Number of Files: 274
Folder: pins_Tom Holland, Number of Files: 302
Folder: pins_Tuppence Middleton, Number of Files: 222
Folder: pins_Ursula Corbero, Number of Files: 172
Folder: pins_Wentworth Miller, Number of Files: 274
Folder: pins_Zac Efron, Number of Files: 292
Folder: pins_Zendaya, Number of Files: 210
Folder: pins_Zoe Saldana, Number of Files: 322
In [21]:
import shutil

folder_path = 'working/extracted_embeddings'

# Check if the folder exists before deleting
if os.path.exists(folder_path):
    shutil.rmtree(folder_path)
    print(f"The folder {folder_path} has been deleted.")
else:
    print(f"The folder {folder_path} does not exist.")
The folder working/extracted_embeddings has been deleted.
In [25]:
import shutil

source_folder = "working/extracted_embeddings"
destination_folder = "working/extracted_embeddingstest"

# Create the destination folder if it doesn't exist
os.makedirs(destination_folder, exist_ok=True)

# Iterate through each folder in the source directory
for folder_name in os.listdir(source_folder):
    folder_path = os.path.join(source_folder, folder_name)

    # Check if it's a directory
    if os.path.isdir(folder_path):
        # Create the corresponding folder in the destination directory
        destination_folder_path = os.path.join(destination_folder, folder_name)
        os.makedirs(destination_folder_path, exist_ok=True)

        # Get the list of files in the current folder
        files = os.listdir(folder_path)

        # Move the first two files that do not contain "augmented" in their names
        moved_files = 0
        for file_name in files:
            if "augmented" not in file_name:
                source_file_path = os.path.join(folder_path, file_name)
                destination_file_path = os.path.join(destination_folder_path, file_name)

                # Move the file (not copy)
                shutil.move(source_file_path, destination_file_path)
                moved_files += 1

                if moved_files == 2:
                    break  # Break after moving two suitable files

print("Files moved successfully.")
Files moved successfully.
In [ ]:
#!pip install face_recognition
In [ ]:
#!pip install facenet-pytorch numpy scikit-learn
In [26]:
from sklearn.metrics import pairwise_distances

# Function to calculate average embedding for each label
def calculate_average_embeddings(base_folder):
    average_embeddings = {}

    for label_folder in os.listdir(base_folder):
        label_path = os.path.join(base_folder, label_folder)
        if os.path.isdir(label_path):
            label_embeddings = []
            for file_name in os.listdir(label_path):
                file_path = os.path.join(label_path, file_name)
                # Load embeddings using your preferred method (e.g., np.load())
                embedding = np.load(file_path)
                label_embeddings.append(embedding)

            # Calculate average embedding for the label
            average_embedding = np.mean(label_embeddings, axis=0)
            average_embeddings[label_folder] = average_embedding

    return average_embeddings

# Function to recognize faces based on existing embeddings
def recognize_faces(test_folder, average_embeddings):
    predictions = []

    for label_folder in os.listdir(test_folder):
        label_path = os.path.join(test_folder, label_folder)
        if os.path.isdir(label_path):
            for file_name in os.listdir(label_path):
                file_path = os.path.join(label_path, file_name)

                # Load test embedding from the saved numpy file
                test_embedding = np.load(file_path)

                # Compare test embedding with averaged embeddings
                distances = {}
                for label, avg_embedding in average_embeddings.items():
                    distance = np.linalg.norm(test_embedding - avg_embedding)
                    distances[label] = distance

                # Predict the label with the minimum distance
                predicted_label = min(distances, key=distances.get)
                predictions.append((file_name, label_folder, predicted_label))

    return predictions


# folder paths
base_folder = 'working/extracted_embeddings'
test_folder = 'working/extracted_embeddingstest'

# Load existing average embeddings
average_embeddings = calculate_average_embeddings(base_folder)

# Recognize faces in the test folder
predictions = recognize_faces(test_folder, average_embeddings)

# Print the predictions
for file_name, actual_label, predicted_label in predictions:
    print(f"{file_name}: Actual Label - {actual_label}, Predicted Label - {predicted_label}")
pins_Adriana Lima_detected_face_0_embedding.npy: Actual Label - pins_Adriana Lima, Predicted Label - pins_Adriana Lima
pins_Adriana Lima_detected_face_101_embedding.npy: Actual Label - pins_Adriana Lima, Predicted Label - pins_Adriana Lima
pins_Alex Lawther_detected_face_214_embedding.npy: Actual Label - pins_Alex Lawther, Predicted Label - pins_Alex Lawther
pins_Alex Lawther_detected_face_215_embedding.npy: Actual Label - pins_Alex Lawther, Predicted Label - pins_Alex Lawther
pins_Alexandra Daddario_detected_face_365_embedding.npy: Actual Label - pins_Alexandra Daddario, Predicted Label - pins_Alexandra Daddario
pins_Alexandra Daddario_detected_face_366_embedding.npy: Actual Label - pins_Alexandra Daddario, Predicted Label - pins_Alexandra Daddario
pins_Alvaro Morte_detected_face_590_embedding.npy: Actual Label - pins_Alvaro Morte, Predicted Label - pins_Alvaro Morte
pins_Alvaro Morte_detected_face_591_embedding.npy: Actual Label - pins_Alvaro Morte, Predicted Label - pins_Alvaro Morte
pins_alycia dabnem carey_detected_face_729_embedding.npy: Actual Label - pins_alycia dabnem carey, Predicted Label - pins_alycia dabnem carey
pins_alycia dabnem carey_detected_face_730_embedding.npy: Actual Label - pins_alycia dabnem carey, Predicted Label - pins_alycia dabnem carey
pins_Amanda Crew_detected_face_1000_embedding.npy: Actual Label - pins_Amanda Crew, Predicted Label - pins_Amanda Crew
pins_Amanda Crew_detected_face_1001_embedding.npy: Actual Label - pins_Amanda Crew, Predicted Label - pins_Amanda Crew
pins_amber heard_detected_face_1057_embedding.npy: Actual Label - pins_amber heard, Predicted Label - pins_amber heard
pins_amber heard_detected_face_1058_embedding.npy: Actual Label - pins_amber heard, Predicted Label - pins_amber heard
pins_Andy Samberg_detected_face_1275_embedding.npy: Actual Label - pins_Andy Samberg, Predicted Label - pins_Andy Samberg
pins_Andy Samberg_detected_face_1276_embedding.npy: Actual Label - pins_Andy Samberg, Predicted Label - pins_Andy Samberg
pins_Anne Hathaway_detected_face_1471_embedding.npy: Actual Label - pins_Anne Hathaway, Predicted Label - pins_Anne Hathaway
pins_Anne Hathaway_detected_face_1472_embedding.npy: Actual Label - pins_Anne Hathaway, Predicted Label - pins_Anne Hathaway
pins_Anthony Mackie_detected_face_1675_embedding.npy: Actual Label - pins_Anthony Mackie, Predicted Label - pins_Anthony Mackie
pins_Anthony Mackie_detected_face_1676_embedding.npy: Actual Label - pins_Anthony Mackie, Predicted Label - pins_Anthony Mackie
pins_Avril Lavigne_detected_face_1798_embedding.npy: Actual Label - pins_Avril Lavigne, Predicted Label - pins_Avril Lavigne
pins_Avril Lavigne_detected_face_1799_embedding.npy: Actual Label - pins_Avril Lavigne, Predicted Label - pins_Avril Lavigne
pins_barack obama_detected_face_1960_embedding.npy: Actual Label - pins_barack obama, Predicted Label - pins_barack obama
pins_barack obama_detected_face_1962_embedding.npy: Actual Label - pins_barack obama, Predicted Label - pins_barack obama
pins_barbara palvin_detected_face_2079_embedding.npy: Actual Label - pins_barbara palvin, Predicted Label - pins_barbara palvin
pins_barbara palvin_detected_face_2080_embedding.npy: Actual Label - pins_barbara palvin, Predicted Label - pins_barbara palvin
pins_Ben Affleck_detected_face_2276_embedding.npy: Actual Label - pins_Ben Affleck, Predicted Label - pins_Ben Affleck
pins_Ben Affleck_detected_face_2278_embedding.npy: Actual Label - pins_Ben Affleck, Predicted Label - pins_Ben Affleck
pins_Bill Gates_detected_face_2404_embedding.npy: Actual Label - pins_Bill Gates, Predicted Label - pins_Bill Gates
pins_Bill Gates_detected_face_2405_embedding.npy: Actual Label - pins_Bill Gates, Predicted Label - pins_Bill Gates
pins_Bobby Morley_detected_face_2524_embedding.npy: Actual Label - pins_Bobby Morley, Predicted Label - pins_Bobby Morley
pins_Bobby Morley_detected_face_2526_embedding.npy: Actual Label - pins_Bobby Morley, Predicted Label - pins_Bobby Morley
pins_Brenton Thwaites_detected_face_2662_embedding.npy: Actual Label - pins_Brenton Thwaites, Predicted Label - pins_Johnny Depp
pins_Brenton Thwaites_detected_face_2663_embedding.npy: Actual Label - pins_Brenton Thwaites, Predicted Label - pins_Brenton Thwaites
pins_Brian J. Smith_detected_face_2871_embedding.npy: Actual Label - pins_Brian J. Smith, Predicted Label - pins_Taylor Swift
pins_Brian J. Smith_detected_face_2873_embedding.npy: Actual Label - pins_Brian J. Smith, Predicted Label - pins_Brian J. Smith
pins_Brie Larson_detected_face_2973_embedding.npy: Actual Label - pins_Brie Larson, Predicted Label - pins_Brie Larson
pins_Brie Larson_detected_face_2974_embedding.npy: Actual Label - pins_Brie Larson, Predicted Label - pins_Brie Larson
pins_camila mendes_detected_face_3142_embedding.npy: Actual Label - pins_camila mendes, Predicted Label - pins_camila mendes
pins_camila mendes_detected_face_3143_embedding.npy: Actual Label - pins_camila mendes, Predicted Label - pins_camila mendes
pins_Chris Evans_detected_face_3304_embedding.npy: Actual Label - pins_Chris Evans, Predicted Label - pins_Chris Evans
pins_Chris Evans_detected_face_3305_embedding.npy: Actual Label - pins_Chris Evans, Predicted Label - pins_Chris Evans
pins_Chris Hemsworth_detected_face_3471_embedding.npy: Actual Label - pins_Chris Hemsworth, Predicted Label - pins_Chris Hemsworth
pins_Chris Hemsworth_detected_face_3472_embedding.npy: Actual Label - pins_Chris Hemsworth, Predicted Label - pins_Chris Hemsworth
pins_Chris Pratt_detected_face_3629_embedding.npy: Actual Label - pins_Chris Pratt, Predicted Label - pins_Chris Pratt
pins_Chris Pratt_detected_face_3630_embedding.npy: Actual Label - pins_Chris Pratt, Predicted Label - pins_Chris Pratt
pins_Christian Bale_detected_face_3805_embedding.npy: Actual Label - pins_Christian Bale, Predicted Label - pins_Johnny Depp
pins_Christian Bale_detected_face_3806_embedding.npy: Actual Label - pins_Christian Bale, Predicted Label - pins_Christian Bale
pins_Cristiano Ronaldo_detected_face_3959_embedding.npy: Actual Label - pins_Cristiano Ronaldo, Predicted Label - pins_Cristiano Ronaldo
pins_Cristiano Ronaldo_detected_face_3960_embedding.npy: Actual Label - pins_Cristiano Ronaldo, Predicted Label - pins_Cristiano Ronaldo
pins_Danielle Panabaker_detected_face_4057_embedding.npy: Actual Label - pins_Danielle Panabaker, Predicted Label - pins_Danielle Panabaker
pins_Danielle Panabaker_detected_face_4058_embedding.npy: Actual Label - pins_Danielle Panabaker, Predicted Label - pins_Elizabeth Lail
pins_Dominic Purcell_detected_face_4239_embedding.npy: Actual Label - pins_Dominic Purcell, Predicted Label - pins_Dominic Purcell
pins_Dominic Purcell_detected_face_4240_embedding.npy: Actual Label - pins_Dominic Purcell, Predicted Label - pins_Dominic Purcell
pins_Dwayne Johnson_detected_face_4385_embedding.npy: Actual Label - pins_Dwayne Johnson, Predicted Label - pins_Dwayne Johnson
pins_Dwayne Johnson_detected_face_4389_embedding.npy: Actual Label - pins_Dwayne Johnson, Predicted Label - pins_Dwayne Johnson
pins_Eliza Taylor_detected_face_4525_embedding.npy: Actual Label - pins_Eliza Taylor, Predicted Label - pins_Eliza Taylor
pins_Eliza Taylor_detected_face_4527_embedding.npy: Actual Label - pins_Eliza Taylor, Predicted Label - pins_Eliza Taylor
pins_Elizabeth Lail_detected_face_4687_embedding.npy: Actual Label - pins_Elizabeth Lail, Predicted Label - pins_Elizabeth Lail
pins_Elizabeth Lail_detected_face_4688_embedding.npy: Actual Label - pins_Elizabeth Lail, Predicted Label - pins_Elizabeth Lail
pins_elizabeth olsen_detected_face_4846_embedding.npy: Actual Label - pins_elizabeth olsen, Predicted Label - pins_elizabeth olsen
pins_elizabeth olsen_detected_face_4847_embedding.npy: Actual Label - pins_elizabeth olsen, Predicted Label - pins_elizabeth olsen
pins_ellen page_detected_face_5066_embedding.npy: Actual Label - pins_ellen page, Predicted Label - pins_ellen page
pins_ellen page_detected_face_5068_embedding.npy: Actual Label - pins_ellen page, Predicted Label - pins_ellen page
pins_elon musk_detected_face_5255_embedding.npy: Actual Label - pins_elon musk, Predicted Label - pins_elon musk
pins_elon musk_detected_face_5256_embedding.npy: Actual Label - pins_elon musk, Predicted Label - pins_elon musk
pins_Emilia Clarke_detected_face_5389_embedding.npy: Actual Label - pins_Emilia Clarke, Predicted Label - pins_Emilia Clarke
pins_Emilia Clarke_detected_face_5391_embedding.npy: Actual Label - pins_Emilia Clarke, Predicted Label - pins_Emilia Clarke
pins_Emma Stone_detected_face_5600_embedding.npy: Actual Label - pins_Emma Stone, Predicted Label - pins_Emma Stone
pins_Emma Stone_detected_face_5601_embedding.npy: Actual Label - pins_Emma Stone, Predicted Label - pins_Emma Stone
pins_Emma Watson_detected_face_5738_embedding.npy: Actual Label - pins_Emma Watson, Predicted Label - pins_Emma Watson
pins_Emma Watson_detected_face_5739_embedding.npy: Actual Label - pins_Emma Watson, Predicted Label - pins_Emma Watson
pins_gal gadot_detected_face_5950_embedding.npy: Actual Label - pins_gal gadot, Predicted Label - pins_gal gadot
pins_gal gadot_detected_face_5951_embedding.npy: Actual Label - pins_gal gadot, Predicted Label - pins_gal gadot
pins_grant gustin_detected_face_6148_embedding.npy: Actual Label - pins_grant gustin, Predicted Label - pins_grant gustin
pins_grant gustin_detected_face_6149_embedding.npy: Actual Label - pins_grant gustin, Predicted Label - pins_grant gustin
pins_Gwyneth Paltrow_detected_face_6331_embedding.npy: Actual Label - pins_Gwyneth Paltrow, Predicted Label - pins_Gwyneth Paltrow
pins_Gwyneth Paltrow_detected_face_6332_embedding.npy: Actual Label - pins_Gwyneth Paltrow, Predicted Label - pins_Gwyneth Paltrow
pins_Henry Cavil_detected_face_6519_embedding.npy: Actual Label - pins_Henry Cavil, Predicted Label - pins_Mark Ruffalo
pins_Henry Cavil_detected_face_6520_embedding.npy: Actual Label - pins_Henry Cavil, Predicted Label - pins_Johnny Depp
pins_Hugh Jackman_detected_face_6713_embedding.npy: Actual Label - pins_Hugh Jackman, Predicted Label - pins_Hugh Jackman
pins_Hugh Jackman_detected_face_6714_embedding.npy: Actual Label - pins_Hugh Jackman, Predicted Label - pins_Hugh Jackman
pins_Inbar Lavi_detected_face_6892_embedding.npy: Actual Label - pins_Inbar Lavi, Predicted Label - pins_Inbar Lavi
pins_Inbar Lavi_detected_face_6893_embedding.npy: Actual Label - pins_Inbar Lavi, Predicted Label - pins_Inbar Lavi
pins_Irina Shayk_detected_face_7019_embedding.npy: Actual Label - pins_Irina Shayk, Predicted Label - pins_Irina Shayk
pins_Irina Shayk_detected_face_7022_embedding.npy: Actual Label - pins_Irina Shayk, Predicted Label - pins_Irina Shayk
pins_Jake Mcdorman_detected_face_7175_embedding.npy: Actual Label - pins_Jake Mcdorman, Predicted Label - pins_Jake Mcdorman
pins_Jake Mcdorman_detected_face_7176_embedding.npy: Actual Label - pins_Jake Mcdorman, Predicted Label - pins_Jake Mcdorman
pins_Jason Momoa_detected_face_7334_embedding.npy: Actual Label - pins_Jason Momoa, Predicted Label - pins_Jason Momoa
pins_Jason Momoa_detected_face_7335_embedding.npy: Actual Label - pins_Jason Momoa, Predicted Label - pins_Jason Momoa
pins_jeff bezos_detected_face_7518_embedding.npy: Actual Label - pins_jeff bezos, Predicted Label - pins_jeff bezos
pins_jeff bezos_detected_face_7519_embedding.npy: Actual Label - pins_jeff bezos, Predicted Label - pins_jeff bezos
pins_Jennifer Lawrence_detected_face_7624_embedding.npy: Actual Label - pins_Jennifer Lawrence, Predicted Label - pins_Jennifer Lawrence
pins_Jennifer Lawrence_detected_face_7625_embedding.npy: Actual Label - pins_Jennifer Lawrence, Predicted Label - pins_Jennifer Lawrence
pins_Jeremy Renner_detected_face_7804_embedding.npy: Actual Label - pins_Jeremy Renner, Predicted Label - pins_Jeremy Renner
pins_Jeremy Renner_detected_face_7805_embedding.npy: Actual Label - pins_Jeremy Renner, Predicted Label - pins_Jeremy Renner
pins_Jessica Barden_detected_face_7971_embedding.npy: Actual Label - pins_Jessica Barden, Predicted Label - pins_Jessica Barden
pins_Jessica Barden_detected_face_7972_embedding.npy: Actual Label - pins_Jessica Barden, Predicted Label - pins_Jessica Barden
pins_Jimmy Fallon_detected_face_8112_embedding.npy: Actual Label - pins_Jimmy Fallon, Predicted Label - pins_Jimmy Fallon
pins_Jimmy Fallon_detected_face_8113_embedding.npy: Actual Label - pins_Jimmy Fallon, Predicted Label - pins_Jimmy Fallon
pins_Johnny Depp_detected_face_8226_embedding.npy: Actual Label - pins_Johnny Depp, Predicted Label - pins_Johnny Depp
pins_Johnny Depp_detected_face_8227_embedding.npy: Actual Label - pins_Johnny Depp, Predicted Label - pins_Johnny Depp
pins_Josh Radnor_detected_face_8407_embedding.npy: Actual Label - pins_Josh Radnor, Predicted Label - pins_Josh Radnor
pins_Josh Radnor_detected_face_8408_embedding.npy: Actual Label - pins_Josh Radnor, Predicted Label - pins_Josh Radnor
pins_Katharine Mcphee_detected_face_8524_embedding.npy: Actual Label - pins_Katharine Mcphee, Predicted Label - pins_Katharine Mcphee
pins_Katharine Mcphee_detected_face_8525_embedding.npy: Actual Label - pins_Katharine Mcphee, Predicted Label - pins_Katharine Mcphee
pins_Katherine Langford_detected_face_8701_embedding.npy: Actual Label - pins_Katherine Langford, Predicted Label - pins_ellen page
pins_Katherine Langford_detected_face_8702_embedding.npy: Actual Label - pins_Katherine Langford, Predicted Label - pins_Katherine Langford
pins_Keanu Reeves_detected_face_8927_embedding.npy: Actual Label - pins_Keanu Reeves, Predicted Label - pins_Keanu Reeves
pins_Keanu Reeves_detected_face_8928_embedding.npy: Actual Label - pins_Keanu Reeves, Predicted Label - pins_Keanu Reeves
pins_kiernen shipka_detected_face_9087_embedding.npy: Actual Label - pins_kiernen shipka, Predicted Label - pins_kiernen shipka
pins_kiernen shipka_detected_face_9088_embedding.npy: Actual Label - pins_kiernen shipka, Predicted Label - pins_kiernen shipka
pins_Krysten Ritter_detected_face_9290_embedding.npy: Actual Label - pins_Krysten Ritter, Predicted Label - pins_Krysten Ritter
pins_Krysten Ritter_detected_face_9291_embedding.npy: Actual Label - pins_Krysten Ritter, Predicted Label - pins_Krysten Ritter
pins_Leonardo DiCaprio_detected_face_9461_embedding.npy: Actual Label - pins_Leonardo DiCaprio, Predicted Label - pins_Leonardo DiCaprio
pins_Leonardo DiCaprio_detected_face_9462_embedding.npy: Actual Label - pins_Leonardo DiCaprio, Predicted Label - pins_Leonardo DiCaprio
pins_Lili Reinhart_detected_face_9698_embedding.npy: Actual Label - pins_Lili Reinhart, Predicted Label - pins_elizabeth olsen
pins_Lili Reinhart_detected_face_9699_embedding.npy: Actual Label - pins_Lili Reinhart, Predicted Label - pins_Lili Reinhart
pins_Lindsey Morgan_detected_face_10001_embedding.npy: Actual Label - pins_Lindsey Morgan, Predicted Label - pins_Lindsey Morgan
pins_Lindsey Morgan_detected_face_10002_embedding.npy: Actual Label - pins_Lindsey Morgan, Predicted Label - pins_Lindsey Morgan
pins_Lionel Messi_detected_face_10017_embedding.npy: Actual Label - pins_Lionel Messi, Predicted Label - pins_Lionel Messi
pins_Lionel Messi_detected_face_10018_embedding.npy: Actual Label - pins_Lionel Messi, Predicted Label - pins_Lionel Messi
pins_Logan Lerman_detected_face_10103_embedding.npy: Actual Label - pins_Logan Lerman, Predicted Label - pins_Logan Lerman
pins_Logan Lerman_detected_face_10104_embedding.npy: Actual Label - pins_Logan Lerman, Predicted Label - pins_Logan Lerman
pins_Madelaine Petsch_detected_face_10316_embedding.npy: Actual Label - pins_Madelaine Petsch, Predicted Label - pins_Madelaine Petsch
pins_Madelaine Petsch_detected_face_10317_embedding.npy: Actual Label - pins_Madelaine Petsch, Predicted Label - pins_Madelaine Petsch
pins_Maisie Williams_detected_face_10509_embedding.npy: Actual Label - pins_Maisie Williams, Predicted Label - pins_Maisie Williams
pins_Maisie Williams_detected_face_10511_embedding.npy: Actual Label - pins_Maisie Williams, Predicted Label - pins_Maisie Williams
pins_margot robbie_detected_face_10700_embedding.npy: Actual Label - pins_margot robbie, Predicted Label - pins_margot robbie
pins_margot robbie_detected_face_10703_embedding.npy: Actual Label - pins_margot robbie, Predicted Label - pins_margot robbie
pins_Maria Pedraza_detected_face_10921_embedding.npy: Actual Label - pins_Maria Pedraza, Predicted Label - pins_Emilia Clarke
pins_Maria Pedraza_detected_face_10922_embedding.npy: Actual Label - pins_Maria Pedraza, Predicted Label - pins_Maria Pedraza
pins_Marie Avgeropoulos_detected_face_11043_embedding.npy: Actual Label - pins_Marie Avgeropoulos, Predicted Label - pins_Marie Avgeropoulos
pins_Marie Avgeropoulos_detected_face_11044_embedding.npy: Actual Label - pins_Marie Avgeropoulos, Predicted Label - pins_Marie Avgeropoulos
pins_Mark Ruffalo_detected_face_11205_embedding.npy: Actual Label - pins_Mark Ruffalo, Predicted Label - pins_Alex Lawther
pins_Mark Ruffalo_detected_face_11207_embedding.npy: Actual Label - pins_Mark Ruffalo, Predicted Label - pins_Mark Ruffalo
pins_Mark Zuckerberg_detected_face_11382_embedding.npy: Actual Label - pins_Mark Zuckerberg, Predicted Label - pins_Mark Zuckerberg
pins_Mark Zuckerberg_detected_face_11383_embedding.npy: Actual Label - pins_Mark Zuckerberg, Predicted Label - pins_Mark Zuckerberg
pins_Megan Fox_detected_face_11477_embedding.npy: Actual Label - pins_Megan Fox, Predicted Label - pins_Megan Fox
pins_Megan Fox_detected_face_11479_embedding.npy: Actual Label - pins_Megan Fox, Predicted Label - pins_Megan Fox
pins_melissa fumero_detected_face_11686_embedding.npy: Actual Label - pins_melissa fumero, Predicted Label - pins_melissa fumero
pins_melissa fumero_detected_face_11687_embedding.npy: Actual Label - pins_melissa fumero, Predicted Label - pins_melissa fumero
pins_Miley Cyrus_detected_face_11842_embedding.npy: Actual Label - pins_Miley Cyrus, Predicted Label - pins_Rihanna
pins_Miley Cyrus_detected_face_11844_embedding.npy: Actual Label - pins_Miley Cyrus, Predicted Label - pins_Miley Cyrus
pins_Millie Bobby Brown_detected_face_12018_embedding.npy: Actual Label - pins_Millie Bobby Brown, Predicted Label - pins_Millie Bobby Brown
pins_Millie Bobby Brown_detected_face_12019_embedding.npy: Actual Label - pins_Millie Bobby Brown, Predicted Label - pins_Millie Bobby Brown
pins_Morena Baccarin_detected_face_12209_embedding.npy: Actual Label - pins_Morena Baccarin, Predicted Label - pins_Morena Baccarin
pins_Morena Baccarin_detected_face_12210_embedding.npy: Actual Label - pins_Morena Baccarin, Predicted Label - pins_Morena Baccarin
pins_Morgan Freeman_detected_face_12386_embedding.npy: Actual Label - pins_Morgan Freeman, Predicted Label - pins_Morgan Freeman
pins_Morgan Freeman_detected_face_12387_embedding.npy: Actual Label - pins_Morgan Freeman, Predicted Label - pins_Morgan Freeman
pins_Nadia Hilker_detected_face_12489_embedding.npy: Actual Label - pins_Nadia Hilker, Predicted Label - pins_Nadia Hilker
pins_Nadia Hilker_detected_face_12493_embedding.npy: Actual Label - pins_Nadia Hilker, Predicted Label - pins_Selena Gomez
pins_Natalie Dormer_detected_face_12623_embedding.npy: Actual Label - pins_Natalie Dormer, Predicted Label - pins_Brie Larson
pins_Natalie Dormer_detected_face_12624_embedding.npy: Actual Label - pins_Natalie Dormer, Predicted Label - pins_Natalie Dormer
pins_Natalie Portman_detected_face_12820_embedding.npy: Actual Label - pins_Natalie Portman, Predicted Label - pins_Natalie Portman
pins_Natalie Portman_detected_face_12821_embedding.npy: Actual Label - pins_Natalie Portman, Predicted Label - pins_Natalie Portman
pins_Neil Patrick Harris_detected_face_12986_embedding.npy: Actual Label - pins_Neil Patrick Harris, Predicted Label - pins_Neil Patrick Harris
pins_Neil Patrick Harris_detected_face_12987_embedding.npy: Actual Label - pins_Neil Patrick Harris, Predicted Label - pins_Robert De Niro
pins_Pedro Alonso_detected_face_13102_embedding.npy: Actual Label - pins_Pedro Alonso, Predicted Label - pins_Pedro Alonso
pins_Pedro Alonso_detected_face_13103_embedding.npy: Actual Label - pins_Pedro Alonso, Predicted Label - pins_Pedro Alonso
pins_Penn Badgley_detected_face_13228_embedding.npy: Actual Label - pins_Penn Badgley, Predicted Label - pins_Penn Badgley
pins_Penn Badgley_detected_face_13229_embedding.npy: Actual Label - pins_Penn Badgley, Predicted Label - pins_Penn Badgley
pins_Rami Malek_detected_face_13398_embedding.npy: Actual Label - pins_Rami Malek, Predicted Label - pins_Robert Downey Jr
pins_Rami Malek_detected_face_13400_embedding.npy: Actual Label - pins_Rami Malek, Predicted Label - pins_Rami Malek
pins_Rebecca Ferguson_detected_face_13558_embedding.npy: Actual Label - pins_Rebecca Ferguson, Predicted Label - pins_Rebecca Ferguson
pins_Rebecca Ferguson_detected_face_13561_embedding.npy: Actual Label - pins_Rebecca Ferguson, Predicted Label - pins_Rebecca Ferguson
pins_Richard Harmon_detected_face_13739_embedding.npy: Actual Label - pins_Richard Harmon, Predicted Label - pins_Richard Harmon
pins_Richard Harmon_detected_face_13741_embedding.npy: Actual Label - pins_Richard Harmon, Predicted Label - pins_Richard Harmon
pins_Rihanna_detected_face_13884_embedding.npy: Actual Label - pins_Rihanna, Predicted Label - pins_Rihanna
pins_Rihanna_detected_face_13885_embedding.npy: Actual Label - pins_Rihanna, Predicted Label - pins_Rihanna
pins_Robert De Niro_detected_face_14017_embedding.npy: Actual Label - pins_Robert De Niro, Predicted Label - pins_Robert De Niro
pins_Robert De Niro_detected_face_14018_embedding.npy: Actual Label - pins_Robert De Niro, Predicted Label - pins_Mark Ruffalo
pins_Robert Downey Jr_detected_face_14174_embedding.npy: Actual Label - pins_Robert Downey Jr, Predicted Label - pins_Robert Downey Jr
pins_Robert Downey Jr_detected_face_14175_embedding.npy: Actual Label - pins_Robert Downey Jr, Predicted Label - pins_Robert Downey Jr
pins_Sarah Wayne Callies_detected_face_14406_embedding.npy: Actual Label - pins_Sarah Wayne Callies, Predicted Label - pins_Sarah Wayne Callies
pins_Sarah Wayne Callies_detected_face_14407_embedding.npy: Actual Label - pins_Sarah Wayne Callies, Predicted Label - pins_Sarah Wayne Callies
pins_scarlett johansson_detected_face_14565_embedding.npy: Actual Label - pins_scarlett johansson, Predicted Label - pins_scarlett johansson
pins_scarlett johansson_detected_face_14568_embedding.npy: Actual Label - pins_scarlett johansson, Predicted Label - pins_scarlett johansson
pins_Selena Gomez_detected_face_14767_embedding.npy: Actual Label - pins_Selena Gomez, Predicted Label - pins_Selena Gomez
pins_Selena Gomez_detected_face_14768_embedding.npy: Actual Label - pins_Selena Gomez, Predicted Label - pins_Selena Gomez
pins_Shakira Isabel Mebarak_detected_face_14952_embedding.npy: Actual Label - pins_Shakira Isabel Mebarak, Predicted Label - pins_Shakira Isabel Mebarak
pins_Shakira Isabel Mebarak_detected_face_14953_embedding.npy: Actual Label - pins_Shakira Isabel Mebarak, Predicted Label - pins_Shakira Isabel Mebarak
pins_Sophie Turner_detected_face_15106_embedding.npy: Actual Label - pins_Sophie Turner, Predicted Label - pins_Sophie Turner
pins_Sophie Turner_detected_face_15107_embedding.npy: Actual Label - pins_Sophie Turner, Predicted Label - pins_Sophie Turner
pins_Stephen Amell_detected_face_15311_embedding.npy: Actual Label - pins_Stephen Amell, Predicted Label - pins_Stephen Amell
pins_Stephen Amell_detected_face_15312_embedding.npy: Actual Label - pins_Stephen Amell, Predicted Label - pins_Stephen Amell
pins_Taylor Swift_detected_face_15474_embedding.npy: Actual Label - pins_Taylor Swift, Predicted Label - pins_Taylor Swift
pins_Taylor Swift_detected_face_15478_embedding.npy: Actual Label - pins_Taylor Swift, Predicted Label - pins_Taylor Swift
pins_Tom Cruise_detected_face_15600_embedding.npy: Actual Label - pins_Tom Cruise, Predicted Label - pins_Tom Cruise
pins_Tom Cruise_detected_face_15601_embedding.npy: Actual Label - pins_Tom Cruise, Predicted Label - pins_Tom Cruise
pins_tom ellis_detected_face_15792_embedding.npy: Actual Label - pins_tom ellis, Predicted Label - pins_tom ellis
pins_tom ellis_detected_face_15794_embedding.npy: Actual Label - pins_tom ellis, Predicted Label - pins_tom ellis
pins_Tom Hardy_detected_face_15973_embedding.npy: Actual Label - pins_Tom Hardy, Predicted Label - pins_Tom Hardy
pins_Tom Hardy_detected_face_15974_embedding.npy: Actual Label - pins_Tom Hardy, Predicted Label - pins_Tom Hardy
pins_Tom Hiddleston_detected_face_16171_embedding.npy: Actual Label - pins_Tom Hiddleston, Predicted Label - pins_Tom Hiddleston
pins_Tom Hiddleston_detected_face_16172_embedding.npy: Actual Label - pins_Tom Hiddleston, Predicted Label - pins_Tom Hiddleston
pins_Tom Holland_detected_face_16351_embedding.npy: Actual Label - pins_Tom Holland, Predicted Label - pins_Tom Holland
pins_Tom Holland_detected_face_16352_embedding.npy: Actual Label - pins_Tom Holland, Predicted Label - pins_Tom Holland
pins_Tuppence Middleton_detected_face_16541_embedding.npy: Actual Label - pins_Tuppence Middleton, Predicted Label - pins_Tuppence Middleton
pins_Tuppence Middleton_detected_face_16543_embedding.npy: Actual Label - pins_Tuppence Middleton, Predicted Label - pins_Tuppence Middleton
pins_Ursula Corbero_detected_face_16677_embedding.npy: Actual Label - pins_Ursula Corbero, Predicted Label - pins_Ursula Corbero
pins_Ursula Corbero_detected_face_16678_embedding.npy: Actual Label - pins_Ursula Corbero, Predicted Label - pins_Ursula Corbero
pins_Wentworth Miller_detected_face_16840_embedding.npy: Actual Label - pins_Wentworth Miller, Predicted Label - pins_Wentworth Miller
pins_Wentworth Miller_detected_face_16841_embedding.npy: Actual Label - pins_Wentworth Miller, Predicted Label - pins_Wentworth Miller
pins_Zac Efron_detected_face_17019_embedding.npy: Actual Label - pins_Zac Efron, Predicted Label - pins_Zac Efron
pins_Zac Efron_detected_face_17020_embedding.npy: Actual Label - pins_Zac Efron, Predicted Label - pins_Zac Efron
pins_Zendaya_detected_face_17210_embedding.npy: Actual Label - pins_Zendaya, Predicted Label - pins_Zendaya
pins_Zendaya_detected_face_17211_embedding.npy: Actual Label - pins_Zendaya, Predicted Label - pins_Leonardo DiCaprio
pins_Zoe Saldana_detected_face_17348_embedding.npy: Actual Label - pins_Zoe Saldana, Predicted Label - pins_Zoe Saldana
pins_Zoe Saldana_detected_face_17349_embedding.npy: Actual Label - pins_Zoe Saldana, Predicted Label - pins_Zoe Saldana
In [27]:
from sklearn.metrics import pairwise_distances
from PIL import Image

# Function to calculate average embedding for each label
def calculate_average_embeddings(base_folder):
    average_embeddings = {}

    for label_folder in os.listdir(base_folder):
        label_path = os.path.join(base_folder, label_folder)
        if os.path.isdir(label_path):
            label_embeddings = []
            for file_name in os.listdir(label_path):
                file_path = os.path.join(label_path, file_name)
                # Load embeddings using your preferred method (e.g., np.load())
                embedding = np.load(file_path)
                label_embeddings.append(embedding)

            # Calculate average embedding for the label
            average_embedding = np.mean(label_embeddings, axis=0)
            average_embeddings[label_folder] = average_embedding

    return average_embeddings

# Function to recognize faces based on existing embeddings
def recognize_faces(test_folder, average_embeddings):
    predictions = []

    for label_folder in os.listdir(test_folder):
        label_path = os.path.join(test_folder, label_folder)
        if os.path.isdir(label_path):
            for file_name in os.listdir(label_path):
                file_path = os.path.join(label_path, file_name)

                # Load test embedding from the saved numpy file
                test_embedding = np.load(file_path)

                # Compare test embedding with averaged embeddings
                distances = {}
                for label, avg_embedding in average_embeddings.items():
                    distance = np.linalg.norm(test_embedding - avg_embedding)
                    distances[label] = distance

                # Predict the label with the minimum distance
                predicted_label = min(distances, key=distances.get)
                predictions.append((file_name, label_folder, predicted_label))

    return predictions

import random

# Function to display images with actual and predicted labels
def display_random_images(predictions, image_folder, num_images=40):
    selected_predictions = random.sample(predictions, min(num_images, len(predictions)))
    
    # Calculate the number of rows and columns based on the desired number of images
    num_rows = (num_images + 4) // 5  # Ensure at least 1 row
    num_cols = min(5, num_images)  # Maximum of 5 columns
    
    fig, axes = plt.subplots(num_rows, num_cols, figsize=(15, 2 * num_rows))
    fig.subplots_adjust(hspace=0.5)  # Adjust the vertical spacing

    for i, (file_name, actual_label, predicted_label) in enumerate(selected_predictions):
        # Extract identifier from the file name
        identifier_start = file_name.find("_detected_face_") + len("_detected_face_")
        identifier_end = file_name.find("_", identifier_start)
        if identifier_end == -1:
            identifier = file_name[identifier_start:]
        else:
            identifier = file_name[identifier_start:identifier_end]

        # Construct the file path for the corresponding image
        image_name = f"detected_face_{identifier}.jpg"
        image_path = os.path.join(image_folder, actual_label, image_name)

        # Display the actual image along with labels
        try:
            image = Image.open(image_path)
            axes[i // 5, i % 5].imshow(image)
            axes[i // 5, i % 5].set_title(f"Actual: {actual_label}\nPredicted: {predicted_label}")
            axes[i // 5, i % 5].axis('off')

        except FileNotFoundError:
            print(f"Image not found for {identifier}. Skipping to the next one.")

    plt.show()

display_random_images(predictions, '/kaggle/working/train_detected_faces', num_images=40)

# folder paths
base_folder = 'working/extracted_embeddings'
test_folder = 'working/extracted_embeddingstest'
image_folder = 'working/train_detected_faces'

# Load existing average embeddings
average_embeddings = calculate_average_embeddings(base_folder)

# Recognize faces in the test folder
predictions = recognize_faces(test_folder, average_embeddings)

# Display 10 images with actual and predicted labels
#display_images(predictions, image_folder, num_images=10)
Image not found for 12623. Skipping to the next one.
Image not found for 101. Skipping to the next one.
Image not found for 11207. Skipping to the next one.
Image not found for 6519. Skipping to the next one.
Image not found for 8701. Skipping to the next one.
Image not found for 591. Skipping to the next one.
Image not found for 7804. Skipping to the next one.
Image not found for 15311. Skipping to the next one.
Image not found for 8408. Skipping to the next one.
Image not found for 6893. Skipping to the next one.
Image not found for 7624. Skipping to the next one.
Image not found for 2276. Skipping to the next one.
Image not found for 2873. Skipping to the next one.
Image not found for 3629. Skipping to the next one.
Image not found for 6714. Skipping to the next one.
Image not found for 16171. Skipping to the next one.
Image not found for 729. Skipping to the next one.
Image not found for 730. Skipping to the next one.
Image not found for 8702. Skipping to the next one.
Image not found for 4385. Skipping to the next one.
Image not found for 16172. Skipping to the next one.
Image not found for 17211. Skipping to the next one.
Image not found for 590. Skipping to the next one.
Image not found for 10700. Skipping to the next one.
Image not found for 17349. Skipping to the next one.
Image not found for 8525. Skipping to the next one.
Image not found for 13398. Skipping to the next one.
Image not found for 9698. Skipping to the next one.
Image not found for 2080. Skipping to the next one.
Image not found for 14406. Skipping to the next one.
Image not found for 1472. Skipping to the next one.
Image not found for 16840. Skipping to the next one.
Image not found for 5389. Skipping to the next one.
Image not found for 11044. Skipping to the next one.
Image not found for 14018. Skipping to the next one.
Image not found for 11844. Skipping to the next one.
Image not found for 2662. Skipping to the next one.
Image not found for 11479. Skipping to the next one.
Image not found for 3630. Skipping to the next one.
Image not found for 1798. Skipping to the next one.
In [29]:
# Display a sample of predictions
print(predictions[:10])
[('pins_Adriana Lima_detected_face_0_embedding.npy', 'pins_Adriana Lima', 'pins_Adriana Lima'), ('pins_Adriana Lima_detected_face_101_embedding.npy', 'pins_Adriana Lima', 'pins_Adriana Lima'), ('pins_Alex Lawther_detected_face_214_embedding.npy', 'pins_Alex Lawther', 'pins_Alex Lawther'), ('pins_Alex Lawther_detected_face_215_embedding.npy', 'pins_Alex Lawther', 'pins_Alex Lawther'), ('pins_Alexandra Daddario_detected_face_365_embedding.npy', 'pins_Alexandra Daddario', 'pins_Alexandra Daddario'), ('pins_Alexandra Daddario_detected_face_366_embedding.npy', 'pins_Alexandra Daddario', 'pins_Alexandra Daddario'), ('pins_Alvaro Morte_detected_face_590_embedding.npy', 'pins_Alvaro Morte', 'pins_Alvaro Morte'), ('pins_Alvaro Morte_detected_face_591_embedding.npy', 'pins_Alvaro Morte', 'pins_Alvaro Morte'), ('pins_alycia dabnem carey_detected_face_729_embedding.npy', 'pins_alycia dabnem carey', 'pins_alycia dabnem carey'), ('pins_alycia dabnem carey_detected_face_730_embedding.npy', 'pins_alycia dabnem carey', 'pins_alycia dabnem carey')]
In [28]:
def load_test_embeddings(test_folder):
    test_embeddings = []

    for label_folder in os.listdir(test_folder):
        label_path = os.path.join(test_folder, label_folder)
        if os.path.isdir(label_path):
            for file_name in os.listdir(label_path):
                file_path = os.path.join(label_path, file_name)

                # Load test embedding from the saved numpy file
                test_embedding = np.load(file_path)
                test_embeddings.append((file_name, label_folder, test_embedding))

    return test_embeddings


# folder paths
base_folder = 'working/extracted_embeddings'
test_folder = 'working/extracted_embeddingstest'

# Load existing average embeddings
average_embeddings = calculate_average_embeddings(base_folder)

# Load test embeddings
test_embeddings = load_test_embeddings(test_folder)

# Randomly pick two test embeddings
random_test_embeddings = random.sample(test_embeddings, 2)

# Print the randomly picked test embeddings
#for file_name, label, test_embedding in random_test_embeddings:
 #   print(f"Randomly Picked Test Embedding ({label}): {test_embedding}")

# Calculate and print distances between the random test embeddings and all average embeddings
for file_name, label, test_embedding in random_test_embeddings:
    print(f"\nDistances for {file_name} ({label}) against Average Embeddings:")
    for avg_label, avg_embedding in average_embeddings.items():
        distance = np.linalg.norm(test_embedding - avg_embedding)
        print(f"  Distance to {avg_label}: {distance}")
Distances for pins_Keanu Reeves_detected_face_8927_embedding.npy (pins_Keanu Reeves) against Average Embeddings:
  Distance to pins_Adriana Lima: 1.0907365083694458
  Distance to pins_Alex Lawther: 1.0727401971817017
  Distance to pins_Alexandra Daddario: 1.212906002998352
  Distance to pins_Alvaro Morte: 0.897905707359314
  Distance to pins_alycia dabnem carey: 1.0393484830856323
  Distance to pins_Amanda Crew: 1.0917949676513672
  Distance to pins_amber heard: 1.1675137281417847
  Distance to pins_Andy Samberg: 1.0244650840759277
  Distance to pins_Anne Hathaway: 1.239278793334961
  Distance to pins_Anthony Mackie: 1.282767415046692
  Distance to pins_Avril Lavigne: 1.2021830081939697
  Distance to pins_barack obama: 1.1130152940750122
  Distance to pins_barbara palvin: 1.106818675994873
  Distance to pins_Ben Affleck: 0.8154085874557495
  Distance to pins_Bill Gates: 1.2312308549880981
  Distance to pins_Bobby Morley: 0.8664924502372742
  Distance to pins_Brenton Thwaites: 0.902674674987793
  Distance to pins_Brian J. Smith: 0.934938371181488
  Distance to pins_Brie Larson: 1.137947916984558
  Distance to pins_camila mendes: 1.3246116638183594
  Distance to pins_Chris Evans: 0.8283305168151855
  Distance to pins_Chris Hemsworth: 0.8055009841918945
  Distance to pins_Chris Pratt: 0.7595720887184143
  Distance to pins_Christian Bale: 0.8515406847000122
  Distance to pins_Cristiano Ronaldo: 1.1392863988876343
  Distance to pins_Danielle Panabaker: 1.2027554512023926
  Distance to pins_Dominic Purcell: 0.9681774973869324
  Distance to pins_Dwayne Johnson: 1.0584139823913574
  Distance to pins_Eliza Taylor: 1.1737382411956787
  Distance to pins_Elizabeth Lail: 1.0739308595657349
  Distance to pins_elizabeth olsen: 1.1441128253936768
  Distance to pins_ellen page: 1.104724645614624
  Distance to pins_elon musk: 1.1403863430023193
  Distance to pins_Emilia Clarke: 1.0858677625656128
  Distance to pins_Emma Stone: 1.1308943033218384
  Distance to pins_Emma Watson: 1.2161176204681396
  Distance to pins_gal gadot: 1.1420031785964966
  Distance to pins_grant gustin: 0.9414496421813965
  Distance to pins_Gwyneth Paltrow: 1.0873854160308838
  Distance to pins_Henry Cavil: 0.8297377824783325
  Distance to pins_Hugh Jackman: 0.8966360688209534
  Distance to pins_Inbar Lavi: 1.2356116771697998
  Distance to pins_Irina Shayk: 1.1176707744598389
  Distance to pins_Jake Mcdorman: 0.9217404723167419
  Distance to pins_Jason Momoa: 0.7688860297203064
  Distance to pins_jeff bezos: 1.2059688568115234
  Distance to pins_Jennifer Lawrence: 1.1183065176010132
  Distance to pins_Jeremy Renner: 1.1273192167282104
  Distance to pins_Jessica Barden: 1.0695838928222656
  Distance to pins_Jimmy Fallon: 0.971354603767395
  Distance to pins_Johnny Depp: 0.9608635306358337
  Distance to pins_Josh Radnor: 0.9516727328300476
  Distance to pins_Katharine Mcphee: 1.1850477457046509
  Distance to pins_Katherine Langford: 1.2283146381378174
  Distance to pins_Keanu Reeves: 0.6936250329017639
  Distance to pins_kiernen shipka: 1.2321311235427856
  Distance to pins_Krysten Ritter: 1.3175181150436401
  Distance to pins_Leonardo DiCaprio: 1.0553410053253174
  Distance to pins_Lili Reinhart: 1.208667278289795
  Distance to pins_Lindsey Morgan: 1.1736204624176025
  Distance to pins_Lionel Messi: 0.8549008965492249
  Distance to pins_Logan Lerman: 1.162579894065857
  Distance to pins_Madelaine Petsch: 1.0973961353302002
  Distance to pins_Maisie Williams: 1.3274699449539185
  Distance to pins_margot robbie: 1.191126823425293
  Distance to pins_Maria Pedraza: 1.1738359928131104
  Distance to pins_Marie Avgeropoulos: 1.1432446241378784
  Distance to pins_Mark Ruffalo: 0.8989967703819275
  Distance to pins_Mark Zuckerberg: 1.1447561979293823
  Distance to pins_Megan Fox: 1.0787358283996582
  Distance to pins_melissa fumero: 1.2268697023391724
  Distance to pins_Miley Cyrus: 1.2401692867279053
  Distance to pins_Millie Bobby Brown: 1.1321072578430176
  Distance to pins_Morena Baccarin: 1.3090741634368896
  Distance to pins_Morgan Freeman: 1.2679976224899292
  Distance to pins_Nadia Hilker: 1.0966815948486328
  Distance to pins_Natalie Dormer: 1.2565295696258545
  Distance to pins_Natalie Portman: 1.2102175951004028
  Distance to pins_Neil Patrick Harris: 0.9708298444747925
  Distance to pins_Pedro Alonso: 1.0211684703826904
  Distance to pins_Penn Badgley: 0.8887699842453003
  Distance to pins_Rami Malek: 1.1131808757781982
  Distance to pins_Rebecca Ferguson: 1.1914513111114502
  Distance to pins_Richard Harmon: 0.9950404763221741
  Distance to pins_Rihanna: 1.1897852420806885
  Distance to pins_Robert De Niro: 0.8974473476409912
  Distance to pins_Robert Downey Jr: 0.933534562587738
  Distance to pins_Sarah Wayne Callies: 1.1564478874206543
  Distance to pins_scarlett johansson: 1.1990810632705688
  Distance to pins_Selena Gomez: 1.166921854019165
  Distance to pins_Shakira Isabel Mebarak: 1.2005335092544556
  Distance to pins_Sophie Turner: 1.2624390125274658
  Distance to pins_Stephen Amell: 0.829555094242096
  Distance to pins_Taylor Swift: 1.1382250785827637
  Distance to pins_Tom Cruise: 0.9140512347221375
  Distance to pins_tom ellis: 0.8193860054016113
  Distance to pins_Tom Hardy: 0.860024094581604
  Distance to pins_Tom Hiddleston: 0.9809429049491882
  Distance to pins_Tom Holland: 1.1067126989364624
  Distance to pins_Tuppence Middleton: 1.2489343881607056
  Distance to pins_Ursula Corbero: 1.0112590789794922
  Distance to pins_Wentworth Miller: 1.0710060596466064
  Distance to pins_Zac Efron: 1.0665520429611206
  Distance to pins_Zendaya: 1.0551358461380005
  Distance to pins_Zoe Saldana: 1.1619575023651123

Distances for pins_melissa fumero_detected_face_11687_embedding.npy (pins_melissa fumero) against Average Embeddings:
  Distance to pins_Adriana Lima: 1.0472183227539062
  Distance to pins_Alex Lawther: 1.1923283338546753
  Distance to pins_Alexandra Daddario: 1.0012543201446533
  Distance to pins_Alvaro Morte: 1.2829509973526
  Distance to pins_alycia dabnem carey: 1.0144710540771484
  Distance to pins_Amanda Crew: 0.9944965839385986
  Distance to pins_amber heard: 1.1784979104995728
  Distance to pins_Andy Samberg: 1.1759744882583618
  Distance to pins_Anne Hathaway: 1.0124280452728271
  Distance to pins_Anthony Mackie: 1.3192522525787354
  Distance to pins_Avril Lavigne: 1.0975881814956665
  Distance to pins_barack obama: 1.2571238279342651
  Distance to pins_barbara palvin: 0.9462469816207886
  Distance to pins_Ben Affleck: 1.201863408088684
  Distance to pins_Bill Gates: 1.3741244077682495
  Distance to pins_Bobby Morley: 1.2897197008132935
  Distance to pins_Brenton Thwaites: 1.4019566774368286
  Distance to pins_Brian J. Smith: 1.2988516092300415
  Distance to pins_Brie Larson: 0.93565833568573
  Distance to pins_camila mendes: 1.1032861471176147
  Distance to pins_Chris Evans: 1.260750651359558
  Distance to pins_Chris Hemsworth: 1.3757761716842651
  Distance to pins_Chris Pratt: 1.2881803512573242
  Distance to pins_Christian Bale: 1.3047698736190796
  Distance to pins_Cristiano Ronaldo: 1.3680692911148071
  Distance to pins_Danielle Panabaker: 0.8331988453865051
  Distance to pins_Dominic Purcell: 1.2457623481750488
  Distance to pins_Dwayne Johnson: 1.3852949142456055
  Distance to pins_Eliza Taylor: 1.0064679384231567
  Distance to pins_Elizabeth Lail: 1.0226492881774902
  Distance to pins_elizabeth olsen: 1.0181124210357666
  Distance to pins_ellen page: 0.9262386560440063
  Distance to pins_elon musk: 1.241856575012207
  Distance to pins_Emilia Clarke: 0.9397671818733215
  Distance to pins_Emma Stone: 0.9803264141082764
  Distance to pins_Emma Watson: 0.9570375680923462
  Distance to pins_gal gadot: 0.9657225012779236
  Distance to pins_grant gustin: 1.3564858436584473
  Distance to pins_Gwyneth Paltrow: 1.1665430068969727
  Distance to pins_Henry Cavil: 1.2245283126831055
  Distance to pins_Hugh Jackman: 1.2988572120666504
  Distance to pins_Inbar Lavi: 1.0178327560424805
  Distance to pins_Irina Shayk: 1.0671101808547974
  Distance to pins_Jake Mcdorman: 1.264137864112854
  Distance to pins_Jason Momoa: 1.3041166067123413
  Distance to pins_jeff bezos: 1.2952908277511597
  Distance to pins_Jennifer Lawrence: 1.0761152505874634
  Distance to pins_Jeremy Renner: 1.24166738986969
  Distance to pins_Jessica Barden: 0.9810200929641724
  Distance to pins_Jimmy Fallon: 1.233288288116455
  Distance to pins_Johnny Depp: 1.2020756006240845
  Distance to pins_Josh Radnor: 1.2048310041427612
  Distance to pins_Katharine Mcphee: 0.9875227808952332
  Distance to pins_Katherine Langford: 0.9426784515380859
  Distance to pins_Keanu Reeves: 1.2889806032180786
  Distance to pins_kiernen shipka: 0.9725674390792847
  Distance to pins_Krysten Ritter: 1.1046295166015625
  Distance to pins_Leonardo DiCaprio: 1.2437162399291992
  Distance to pins_Lili Reinhart: 1.0465792417526245
  Distance to pins_Lindsey Morgan: 0.9623363018035889
  Distance to pins_Lionel Messi: 1.3091732263565063
  Distance to pins_Logan Lerman: 1.3325268030166626
  Distance to pins_Madelaine Petsch: 0.9654428362846375
  Distance to pins_Maisie Williams: 1.0539387464523315
  Distance to pins_margot robbie: 1.0828676223754883
  Distance to pins_Maria Pedraza: 0.9950555562973022
  Distance to pins_Marie Avgeropoulos: 1.0161763429641724
  Distance to pins_Mark Ruffalo: 1.1699845790863037
  Distance to pins_Mark Zuckerberg: 1.284671664237976
  Distance to pins_Megan Fox: 0.9973228573799133
  Distance to pins_melissa fumero: 0.4341469705104828
  Distance to pins_Miley Cyrus: 1.0216658115386963
  Distance to pins_Millie Bobby Brown: 0.9451692700386047
  Distance to pins_Morena Baccarin: 1.0219240188598633
  Distance to pins_Morgan Freeman: 1.3772648572921753
  Distance to pins_Nadia Hilker: 1.1397740840911865
  Distance to pins_Natalie Dormer: 1.0432283878326416
  Distance to pins_Natalie Portman: 0.8651353120803833
  Distance to pins_Neil Patrick Harris: 1.293210506439209
  Distance to pins_Pedro Alonso: 1.1097618341445923
  Distance to pins_Penn Badgley: 1.2915738821029663
  Distance to pins_Rami Malek: 1.2150154113769531
  Distance to pins_Rebecca Ferguson: 0.9449279308319092
  Distance to pins_Richard Harmon: 1.245517611503601
  Distance to pins_Rihanna: 1.263484001159668
  Distance to pins_Robert De Niro: 1.2608880996704102
  Distance to pins_Robert Downey Jr: 1.2616602182388306
  Distance to pins_Sarah Wayne Callies: 0.8238881230354309
  Distance to pins_scarlett johansson: 1.100235104560852
  Distance to pins_Selena Gomez: 0.999914288520813
  Distance to pins_Shakira Isabel Mebarak: 1.1549603939056396
  Distance to pins_Sophie Turner: 0.975644588470459
  Distance to pins_Stephen Amell: 1.2820969820022583
  Distance to pins_Taylor Swift: 1.0795440673828125
  Distance to pins_Tom Cruise: 1.2875176668167114
  Distance to pins_tom ellis: 1.2337234020233154
  Distance to pins_Tom Hardy: 1.2114332914352417
  Distance to pins_Tom Hiddleston: 1.2515019178390503
  Distance to pins_Tom Holland: 1.2163454294204712
  Distance to pins_Tuppence Middleton: 0.8098502159118652
  Distance to pins_Ursula Corbero: 1.0663021802902222
  Distance to pins_Wentworth Miller: 1.3005812168121338
  Distance to pins_Zac Efron: 1.3336941003799438
  Distance to pins_Zendaya: 1.196130633354187
  Distance to pins_Zoe Saldana: 1.1714837551116943
In [30]:
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

# Disable CUDA
torch.cuda.is_available = lambda : False

# Set the device to CPU
device = torch.device("cpu")

# Load embeddings and labels
data_dir = "working/extracted_embeddings"
embeddings = []
labels = []

for label_folder in os.listdir(data_dir):
    label_path = os.path.join(data_dir, label_folder)
    if os.path.isdir(label_path):
        label = label_folder  # Assuming the folder names are labels
        embeddings_per_label = [] 

        for file_name in os.listdir(label_path):
            file_path = os.path.join(label_path, file_name)
            if file_name.endswith(".npy"):
                embedding = np.load(file_path)
                embeddings_per_label.append(embedding)
                labels.append(label)

        embeddings.append(embeddings_per_label)

# Flatten the embeddings list
embeddings = [item for sublist in embeddings for item in sublist]

# Convert data to PyTorch tensors
X = torch.tensor(embeddings, dtype=torch.float32)
labels = np.array(labels)

# Use LabelEncoder to encode string labels into integers
label_encoder = LabelEncoder()
y = torch.tensor(label_encoder.fit_transform(labels), dtype=torch.long)

# Split the data into 80% training, 10% validation, and 10% test
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.2, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)

# Define an updated model with more layers
class UpdatedModel(nn.Module):
    def __init__(self, input_size, num_classes):
        super(UpdatedModel, self).__init__()
        self.fc1 = nn.Linear(input_size, 256)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Linear(256, 128)
        self.relu2 = nn.ReLU()
        self.fc3 = nn.Linear(128, 64)
        self.relu3 = nn.ReLU()
        self.fc4 = nn.Linear(64, num_classes)

    def forward(self, x):
        x = self.fc1(x)
        x = self.relu1(x)
        x = self.fc2(x)
        x = self.relu2(x)
        x = self.fc3(x)
        x = self.relu3(x)
        x = self.fc4(x)
        return x

# Initialize the model
input_size = X_train.shape[1]
num_classes = len(set(y_train))
model = UpdatedModel(input_size, num_classes).to(device)

# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training loop
num_epochs = 20
batch_size = 32

for epoch in range(num_epochs):
    model.train()
    for i in range(0, len(X_train), batch_size):
        inputs = X_train[i:i+batch_size].to(device)
        labels = y_train[i:i+batch_size].to(device)

        optimizer.zero_grad()

        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

    # Validation
    model.eval()
    with torch.no_grad():
        val_inputs = X_val.to(device)
        val_labels = y_val.to(device)

        val_outputs = model(val_inputs)
        val_loss = criterion(val_outputs, val_labels)

    print(f"Epoch {epoch+1}/{num_epochs}, Train Loss: {loss.item():.4f}, Validation Loss: {val_loss.item():.4f}")

# Evaluation on the test data
model.eval()
with torch.no_grad():
    test_inputs = X_test.to(device)
    test_labels = y_test.to(device)

    test_outputs = model(test_inputs)
    test_loss = criterion(test_outputs, test_labels)

# Decode the predicted labels using inverse_transform
predicted_labels = label_encoder.inverse_transform(torch.argmax(test_outputs, dim=1).cpu().numpy())

# Decode the true labels using inverse_transform
true_labels = label_encoder.inverse_transform(y_test.cpu().numpy())

# Display classification report and confusion matrix
print("Classification Report:")
print(classification_report(true_labels, predicted_labels))

#print("Confusion Matrix:")
#print(confusion_matrix(true_labels, predicted_labels))
C:\Users\Ifham.Khan\AppData\Local\Temp\ipykernel_7464\3096603719.py:39: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\utils\tensor_new.cpp:264.)
  X = torch.tensor(embeddings, dtype=torch.float32)
Epoch 1/20, Train Loss: 1.1412, Validation Loss: 1.3178
Epoch 2/20, Train Loss: 0.6614, Validation Loss: 0.7568
Epoch 3/20, Train Loss: 0.4601, Validation Loss: 0.5850
Epoch 4/20, Train Loss: 0.3778, Validation Loss: 0.4945
Epoch 5/20, Train Loss: 0.3405, Validation Loss: 0.4458
Epoch 6/20, Train Loss: 0.3273, Validation Loss: 0.4145
Epoch 7/20, Train Loss: 0.3195, Validation Loss: 0.3889
Epoch 8/20, Train Loss: 0.3022, Validation Loss: 0.3683
Epoch 9/20, Train Loss: 0.2806, Validation Loss: 0.3599
Epoch 10/20, Train Loss: 0.2503, Validation Loss: 0.3586
Epoch 11/20, Train Loss: 0.2537, Validation Loss: 0.3603
Epoch 12/20, Train Loss: 0.2762, Validation Loss: 0.3651
Epoch 13/20, Train Loss: 0.2950, Validation Loss: 0.3817
Epoch 14/20, Train Loss: 0.1758, Validation Loss: 0.4054
Epoch 15/20, Train Loss: 0.1757, Validation Loss: 0.4104
Epoch 16/20, Train Loss: 0.1522, Validation Loss: 0.3856
Epoch 17/20, Train Loss: 0.1891, Validation Loss: 0.3679
Epoch 18/20, Train Loss: 0.1751, Validation Loss: 0.3812
Epoch 19/20, Train Loss: 0.0899, Validation Loss: 0.4209
Epoch 20/20, Train Loss: 0.0527, Validation Loss: 0.4481
Classification Report:
                             precision    recall  f1-score   support

          pins_Adriana Lima       1.00      0.94      0.97        33
          pins_Alex Lawther       0.89      0.89      0.89        18
    pins_Alexandra Daddario       1.00      0.90      0.95        42
          pins_Alvaro Morte       0.77      0.94      0.85        18
           pins_Amanda Crew       1.00      0.95      0.98        22
          pins_Andy Samberg       0.94      0.81      0.87        36
         pins_Anne Hathaway       0.83      0.97      0.90        40
        pins_Anthony Mackie       1.00      1.00      1.00        21
         pins_Avril Lavigne       1.00      0.95      0.98        21
           pins_Ben Affleck       0.82      0.90      0.86        20
            pins_Bill Gates       1.00      1.00      1.00        15
          pins_Bobby Morley       0.62      0.67      0.65        15
      pins_Brenton Thwaites       0.95      0.93      0.94        41
        pins_Brian J. Smith       0.94      0.94      0.94        16
           pins_Brie Larson       0.69      0.87      0.77        23
           pins_Chris Evans       0.95      0.77      0.85        26
       pins_Chris Hemsworth       0.89      0.89      0.89        27
           pins_Chris Pratt       1.00      0.93      0.96        40
        pins_Christian Bale       1.00      0.85      0.92        20
     pins_Cristiano Ronaldo       1.00      0.91      0.95        11
    pins_Danielle Panabaker       1.00      0.97      0.99        34
       pins_Dominic Purcell       0.96      1.00      0.98        25
        pins_Dwayne Johnson       1.00      0.78      0.88         9
          pins_Eliza Taylor       0.77      1.00      0.87        24
        pins_Elizabeth Lail       0.86      0.63      0.73        19
         pins_Emilia Clarke       0.82      0.97      0.89        34
            pins_Emma Stone       1.00      0.88      0.93        16
           pins_Emma Watson       0.97      0.97      0.97        37
       pins_Gwyneth Paltrow       0.96      0.92      0.94        26
           pins_Henry Cavil       1.00      0.65      0.79        20
          pins_Hugh Jackman       1.00      0.97      0.98        30
            pins_Inbar Lavi       1.00      0.86      0.93        22
           pins_Irina Shayk       0.89      1.00      0.94        17
         pins_Jake Mcdorman       1.00      1.00      1.00        26
           pins_Jason Momoa       0.96      0.96      0.96        24
     pins_Jennifer Lawrence       1.00      0.96      0.98        23
         pins_Jeremy Renner       1.00      1.00      1.00        30
        pins_Jessica Barden       1.00      0.76      0.86        29
          pins_Jimmy Fallon       0.95      0.73      0.83        26
           pins_Johnny Depp       0.97      0.94      0.95        31
           pins_Josh Radnor       0.73      0.92      0.81        12
      pins_Katharine Mcphee       0.96      0.96      0.96        25
    pins_Katherine Langford       0.95      0.86      0.90        42
          pins_Keanu Reeves       0.92      0.92      0.92        25
        pins_Krysten Ritter       1.00      0.87      0.93        30
     pins_Leonardo DiCaprio       0.89      0.89      0.89        44
         pins_Lili Reinhart       0.79      0.95      0.86        20
        pins_Lindsey Morgan       0.84      0.84      0.84        25
          pins_Lionel Messi       1.00      0.85      0.92        13
          pins_Logan Lerman       0.93      0.97      0.95        39
      pins_Madelaine Petsch       0.97      0.97      0.97        31
       pins_Maisie Williams       0.92      0.96      0.94        24
         pins_Maria Pedraza       0.83      0.71      0.77        14
    pins_Marie Avgeropoulos       0.96      1.00      0.98        23
          pins_Mark Ruffalo       0.63      1.00      0.77        24
       pins_Mark Zuckerberg       0.93      0.93      0.93        15
             pins_Megan Fox       1.00      0.85      0.92        34
           pins_Miley Cyrus       0.89      0.94      0.91        17
    pins_Millie Bobby Brown       0.89      0.83      0.86        30
       pins_Morena Baccarin       0.90      1.00      0.95        27
        pins_Morgan Freeman       0.83      0.83      0.83         6
          pins_Nadia Hilker       0.88      0.93      0.90        15
        pins_Natalie Dormer       0.97      0.97      0.97        32
       pins_Natalie Portman       1.00      0.90      0.95        20
   pins_Neil Patrick Harris       0.88      0.88      0.88        16
          pins_Pedro Alonso       0.94      1.00      0.97        15
          pins_Penn Badgley       0.97      0.97      0.97        30
            pins_Rami Malek       0.94      0.91      0.93        35
      pins_Rebecca Ferguson       0.89      0.91      0.90        44
        pins_Richard Harmon       0.75      0.90      0.82        20
               pins_Rihanna       0.88      0.94      0.91        16
        pins_Robert De Niro       0.60      0.89      0.72        27
      pins_Robert Downey Jr       0.83      0.93      0.88        43
   pins_Sarah Wayne Callies       0.83      0.86      0.84        22
          pins_Selena Gomez       0.86      0.86      0.86        22
pins_Shakira Isabel Mebarak       0.96      0.90      0.93        29
         pins_Sophie Turner       0.97      0.94      0.95        32
         pins_Stephen Amell       1.00      0.92      0.96        36
          pins_Taylor Swift       1.00      1.00      1.00         9
            pins_Tom Cruise       0.93      0.97      0.95        29
             pins_Tom Hardy       0.81      0.85      0.83        20
        pins_Tom Hiddleston       0.96      0.79      0.86        28
           pins_Tom Holland       0.95      0.93      0.94        42
    pins_Tuppence Middleton       1.00      1.00      1.00        15
        pins_Ursula Corbero       1.00      0.90      0.95        21
      pins_Wentworth Miller       0.81      0.96      0.88        26
             pins_Zac Efron       0.97      1.00      0.98        30
               pins_Zendaya       0.88      0.95      0.91        22
           pins_Zoe Saldana       1.00      0.97      0.99        35
   pins_alycia dabnem carey       0.85      0.97      0.91        30
           pins_amber heard       0.96      0.93      0.94        27
          pins_barack obama       0.92      1.00      0.96        12
        pins_barbara palvin       0.87      0.97      0.92        35
         pins_camila mendes       0.86      0.95      0.90        20
       pins_elizabeth olsen       0.97      0.93      0.95        40
            pins_ellen page       0.97      0.97      0.97        29
             pins_elon musk       0.95      0.90      0.92        20
             pins_gal gadot       0.92      0.88      0.90        25
          pins_grant gustin       1.00      0.96      0.98        24
            pins_jeff bezos       1.00      1.00      1.00         8
        pins_kiernen shipka       1.00      0.96      0.98        28
         pins_margot robbie       1.00      0.88      0.94        25
        pins_melissa fumero       1.00      0.93      0.96        27
    pins_scarlett johansson       0.95      0.95      0.95        38
             pins_tom ellis       0.96      0.92      0.94        26

                   accuracy                           0.92      2672
                  macro avg       0.92      0.91      0.91      2672
               weighted avg       0.93      0.92      0.92      2672

In [ ]:
#pip install facenet-pytorch opencv-python matplotlib
In [39]:
import os
import random
from facenet_pytorch import InceptionResnetV1, MTCNN
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import numpy as np
from sklearn.metrics.pairwise import euclidean_distances
import torch

# Function to load actor embeddings and names
def load_actor_data(embeddings_directory):
    actor_embeddings = []
    actor_names = []

    for actor_folder in os.listdir(embeddings_directory):
        actor_folder_path = os.path.join(embeddings_directory, actor_folder)

        # Assuming each actor's folder contains files like "pins_ActorName_detected_face_*.npy"
        for embedding_file in os.listdir(actor_folder_path):
            embedding_file_path = os.path.join(actor_folder_path, embedding_file)

            # Load the embedding
            embedding = np.load(embedding_file_path)

            # Append the embedding and actor name
            actor_embeddings.append(embedding)
            actor_names.append(actor_folder)

    return np.array(actor_embeddings), np.array(actor_names)

# Function to process images and calculate embeddings
def process_images(frames_directory, face_embedder, mtcnn, actor_embeddings, actor_names):
    # List all files in the frames directory
    frame_files = os.listdir(frames_directory)

    # Pick two random files from the frames directory
    face1_file, face2_file = np.random.choice(frame_files, 2, replace=False)

    # Read the face images
    face1_path = os.path.join(frames_directory, face1_file)
    face2_path = os.path.join(frames_directory, face2_file)

    face1 = cv2.imread(face1_path)
    face1_rgb = cv2.cvtColor(face1, cv2.COLOR_BGR2RGB)

    face2 = cv2.imread(face2_path)
    face2_rgb = cv2.cvtColor(face2, cv2.COLOR_BGR2RGB)

    # Perform face detection using MTCNN
    boxes1, probs1 = mtcnn.detect(face1_rgb)
    boxes2, probs2 = mtcnn.detect(face2_rgb)

    if boxes1 is not None and boxes2 is not None:
        # Extract faces using the bounding boxes
        face1_box = boxes1[0].astype(int)
        face1_rgb = face1_rgb[face1_box[1]:face1_box[3], face1_box[0]:face1_box[2]]

        face2_box = boxes2[0].astype(int)
        face2_rgb = face2_rgb[face2_box[1]:face2_box[3], face2_box[0]:face2_box[2]]

        # Display original face images
        plt.subplot(1, 2, 1)
        plt.imshow(Image.fromarray(face1_rgb))
        plt.title(f"Original Face 1\nFrame: {face1_file}")
        plt.axis('off')

        plt.subplot(1, 2, 2)
        plt.imshow(Image.fromarray(face2_rgb))
        plt.title(f"Original Face 2\nFrame: {face2_file}")
        plt.axis('off')

        plt.show()

        # Convert NumPy arrays to PyTorch tensors
        face1_tensor = torch.from_numpy(face1_rgb.transpose(2, 0, 1)).unsqueeze(0).float() / 255.0
        face2_tensor = torch.from_numpy(face2_rgb.transpose(2, 0, 1)).unsqueeze(0).float() / 255.0

        # Calculate embeddings for the faces
        embeddings1 = face_embedder(face1_tensor).detach().numpy()
        embeddings2 = face_embedder(face2_tensor).detach().numpy()

        # Calculate distance between the two embeddings
        distance = np.linalg.norm(embeddings1 - embeddings2)

        # Find the actor with the closest embedding
        min_distance_index = np.argmin(euclidean_distances(embeddings1, actor_embeddings))
        actor_name = actor_names[min_distance_index]

        # Display the distance between the two embeddings
        print(f"Distance between embeddings: {distance:.4f}")
        print(f"Predicted actor: {actor_name}")
    else:
        print("Face not detected in one or both of the images.")

# Directory paths
frames_directory = 'C:\\Users\\Ifham.Khan\\Desktop\\Final Year Project\\105_classes_pins_dataset\\pins_Emma Watson'
embeddings_directory = 'working/extracted_embeddings'

# Initialize MTCNN for face detection
mtcnn = MTCNN(keep_all=True)

# Initialize InceptionResnetV1 for face embedding
face_embedder = InceptionResnetV1(pretrained='vggface2').eval()

# Load actor embeddings and names
actor_embeddings, actor_names = load_actor_data(embeddings_directory)

# Process face images, calculate embeddings, and display results
process_images(frames_directory, face_embedder, mtcnn, actor_embeddings, actor_names)
Distance between embeddings: 1.0611
Predicted actor: pins_Emma Watson
In [40]:
import os

# Define the directory path
save_directory = r'C:\Users\Ifham.Khan\Desktop\Final Year Project'

# Define the file name
file_name = 'Face_detection.pth'

# Combine directory path and file name
model_save_path = os.path.join(save_directory, file_name)

# Save the model's architecture and parameters
torch.save(model.state_dict(), model_save_path)

print(f"Model saved at {model_save_path}")
Model saved at C:\Users\Ifham.Khan\Desktop\Final Year Project\Face_detection.pth
In [ ]: